Skip to contentSkip to conversation

KVM in-guest performance monitoring

PATCH Superseded Avi Kivity 5 participants 40 messages May 11, 2011 – Jun 5, 2011

Subsystems (8)
Arm (32-bit) x86 KVM PowerPC +4 more subsystems

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Avi Kivity

author
#0 lore raw

cover KVM in-guest performance monitoring

This not-for-merging patchset exposes an emulated version 1 architectural performance monitoring unit to KVM guests. The PMU is emulated using perf_events, so the host kernel can multiplex host-wide, host-user, and the guest on available resources.

Caveats:
- counters that have PMI (interrupt) enabled stop counting after the interrupt is signalled. This is because we need one-shot samples that keep counting, which perf doesn't support yet
- some combinations of INV and CMASK are not supported
- counters keep on counting in the host as well as the guest
- the RDPMC instruction and CR4.PCE bit are not yet emulated
- there is likely a bug in the implementation; running 'perf top' in a guest that spends 80% of its time in userspace shows perf itself as consuming almost all cpu

perf maintainers: please consider the first three patches for merging (the first two make sense even without the rest). If you're familiar with the Intel PMU, please review patch 5 as well - it effectively undoes all your work of abstracting the PMU into perf_events by unabstracting perf_events into what is hoped is a very similar PMU.

Avi Kivity (5):
perf: add context parameter to perf_event overflow handler x86, perf: add constraints for architectural PMU v1
perf: export perf_event_refresh() to modules
KVM: Expose kvm_lapic_local_deliver()
KVM: Expose a version 1 architectural PMU to guests

 arch/arm/kernel/ptrace.c                |   10 +-
 arch/powerpc/kernel/ptrace.c            |    4 +-
 arch/sh/kernel/ptrace_32.c              |    5 +-
 arch/x86/include/asm/kvm_host.h         |   29 ++++
 arch/x86/kernel/cpu/perf_event_intel.c  |   23 +++-
 arch/x86/kernel/kgdb.c                  |    2 +-
 arch/x86/kernel/ptrace.c                |    5 +-
 arch/x86/kvm/Makefile                   |    2 +-
 arch/x86/kvm/lapic.c                    |    2 +-
 arch/x86/kvm/lapic.h                    |    1 +
 arch/x86/kvm/pmu.c                      |  248 +++++++++++++++++++++++++++++++
 arch/x86/kvm/x86.c                      |   16 +-
 drivers/oprofile/oprofile_perf.c        |    5 +-
 include/linux/hw_breakpoint.h           |   10 +-
 include/linux/perf_event.h              |   13 ++-
 kernel/events/core.c                    |   27 +++-
 kernel/events/hw_breakpoint.c           |   10 +-
 kernel/watchdog.c                       |    7 +-
 samples/hw_breakpoint/data_breakpoint.c |    5 +-
 19 files changed, 377 insertions(+), 47 deletions(-)
 create mode 100644 arch/x86/kvm/pmu.c
Signature · 1 line
1.7.4.3
author
#4 lore raw

PATCH 2/5 x86, perf: add constraints for architectural PMU v1

The v1 PMU does not have any fixed counters. Using the v2 constraints, which do have fixed counters, causes an additional choice to be present in the weight calculation, but not when actually scheduling the event, leading to an event being not scheduled at all.

---
 arch/x86/kernel/cpu/perf_event_intel.c |   23 ++++++++++++++++++-----
 1 files changed, 18 insertions(+), 5 deletions(-)
arch/x86/kernel/cpu/perf_event_intel.c +18 -5
index 41178c8..b46b70e 100644
@@ -137,6 +137,11 @@ static struct event_constraint intel_westmere_percore_constraints[] __read_mostl
 	EVENT_CONSTRAINT_END
 };
 
+static struct event_constraint intel_v1_event_constraints[] __read_mostly =
+{
+	EVENT_CONSTRAINT_END
+};
+
 static struct event_constraint intel_gen_event_constraints[] __read_mostly =
 {
 	FIXED_EVENT_CONSTRAINT(0x00c0, 0), /* INST_RETIRED.ANY */
@@ -1512,11 +1517,19 @@ static __init int intel_pmu_init(void)
 		break;
 
 	default:
-		/*
-		 * default constraints for v2 and up
-		 */
-		x86_pmu.event_constraints = intel_gen_event_constraints;
-		pr_cont("generic architected perfmon, ");
+		switch (x86_pmu.version) {
+		case 1:
+			x86_pmu.event_constraints = intel_v1_event_constraints;
+			pr_cont("generic architected perfmon v1, ");
+			break;
+		default:
+			/*
+			 * default constraints for v2 and up
+			 */
+			x86_pmu.event_constraints = intel_gen_event_constraints;
+			pr_cont("generic architected perfmon, ");
+			break;
+		}
 	}
 	return 0;
 }
Signature · 1 line
1.7.4.3
author
#5 lore raw

PATCH 3/5 perf: export perf_event_refresh() to modules

In reply to Avi Kivity

KVM needs one-shot samples, since a PMC programmed to -X will fire after X events and then again after 2^40 events (i.e. variable period).

---
 include/linux/perf_event.h |    5 +++++
 kernel/events/core.c       |    3 ++-
 2 files changed, 7 insertions(+), 1 deletions(-)
include/linux/perf_event.h +5 -0
index de346a0..b317a8b 100644
@@ -975,6 +975,7 @@ extern void perf_pmu_disable(struct pmu *pmu);
 extern void perf_pmu_enable(struct pmu *pmu);
 extern int perf_event_task_disable(void);
 extern int perf_event_task_enable(void);
+extern int perf_event_refresh(struct perf_event *event, int refresh);
 extern void perf_event_update_userpage(struct perf_event *event);
 extern int perf_event_release_kernel(struct perf_event *event);
 extern struct perf_event *
@@ -1170,6 +1171,10 @@ static inline void perf_event_delayed_put(struct task_struct *task)	{ }
 static inline void perf_event_print_debug(void)				{ }
 static inline int perf_event_task_disable(void)				{ return -EINVAL; }
 static inline int perf_event_task_enable(void)				{ return -EINVAL; }
+static inline int perf_event_refresh(struct perf_event *event, int refresh)
+{
+	return -EINVAL;
+}
 
 static inline void
 perf_sw_event(u32 event_id, u64 nr, int nmi,
kernel/events/core.c +2 -1
index f60b81c..6f58291 100644
@@ -1747,7 +1747,7 @@ out:
 	raw_spin_unlock_irq(&ctx->lock);
 }
 
-static int perf_event_refresh(struct perf_event *event, int refresh)
+int perf_event_refresh(struct perf_event *event, int refresh)
 {
 	/*
 	 * not supported on inherited events
@@ -1760,6 +1760,7 @@ static int perf_event_refresh(struct perf_event *event, int refresh)
 
 	return 0;
 }
+EXPORT_SYMBOL_GPL(perf_event_refresh);
 
 static void ctx_sched_out(struct perf_event_context *ctx,
 			  struct perf_cpu_context *cpuctx,
Signature · 1 line
1.7.4.3
author
#6 lore raw

PATCH 4/5 KVM: Expose kvm_lapic_local_deliver()

In reply to Avi Kivity

Needed to deliver performance monitoring interrupts.

---
 arch/x86/kvm/lapic.c |    2 +-
 arch/x86/kvm/lapic.h |    1 +
 2 files changed, 2 insertions(+), 1 deletions(-)
arch/x86/kvm/lapic.c +1 -1
index 2b2255b..79d4850 100644
@@ -1017,7 +1017,7 @@ int apic_has_pending_timer(struct kvm_vcpu *vcpu)
 	return 0;
 }
 
-static int kvm_apic_local_deliver(struct kvm_lapic *apic, int lvt_type)
+int kvm_apic_local_deliver(struct kvm_lapic *apic, int lvt_type)
 {
 	u32 reg = apic_get_reg(apic, lvt_type);
 	int vector, mode, trig_mode;
arch/x86/kvm/lapic.h +1 -0
index 52c9e6b..09dbe2f 100644
@@ -33,6 +33,7 @@ void kvm_apic_set_version(struct kvm_vcpu *vcpu);
 int kvm_apic_match_physical_addr(struct kvm_lapic *apic, u16 dest);
 int kvm_apic_match_logical_addr(struct kvm_lapic *apic, u8 mda);
 int kvm_apic_set_irq(struct kvm_vcpu *vcpu, struct kvm_lapic_irq *irq);
+int kvm_apic_local_deliver(struct kvm_lapic *apic, int lvt_type);
 
 u64 kvm_get_apic_base(struct kvm_vcpu *vcpu);
 void kvm_set_apic_base(struct kvm_vcpu *vcpu, u64 data);
Signature · 1 line
1.7.4.3
author
#7 lore raw

PATCH 5/5 KVM: Expose a version 1 architectural PMU to guests

In reply to Avi Kivity

Use perf_events to emulate an architectural PMU, version 1.

Caveats:
- counters that have PMI (interrupt) enabled stop counting after the interrupt is signalled. This is because we need one-shot samples that keep counting, which perf doesn't support yet
- some combinations of INV and CMASK are not supported
- counters keep on counting in the host as well as the guest

---
 arch/x86/include/asm/kvm_host.h |   29 +++++
 arch/x86/kvm/Makefile           |    2 +-
 arch/x86/kvm/pmu.c              |  248 +++++++++++++++++++++++++++++++++++++++
 arch/x86/kvm/x86.c              |   16 ++--
 4 files changed, 286 insertions(+), 9 deletions(-)
 create mode 100644 arch/x86/kvm/pmu.c
arch/x86/include/asm/kvm_host.h +29 -0
index d2ac8e2..5563fb4 100644
@@ -16,6 +16,7 @@
 #include <linux/mmu_notifier.h>
 #include <linux/tracepoint.h>
 #include <linux/cpumask.h>
+#include <linux/irq_work.h>
 
 #include <linux/kvm.h>
 #include <linux/kvm_para.h>
@@ -291,6 +292,24 @@ struct kvm_mmu {
 	u64 pdptrs[4]; /* pae */
 };
 
+#define KVM_PMU_MAX_GENERAL_PURPOSE_COUNTERS 4
+
+struct kvm_pmc {
+	u64 counter;
+	u64 eventsel;
+	struct perf_event *perf_event;
+	struct kvm_vcpu *vcpu;
+};
+
+struct kvm_pmu {
+	unsigned nr_arch_gp_counters;
+	unsigned available_event_types;
+	u64 counter_bitmask;
+	u8 version;
+	struct kvm_pmc gp_counters[KVM_PMU_MAX_GENERAL_PURPOSE_COUNTERS];
+	struct irq_work irq_work;
+};
+
 struct kvm_vcpu_arch {
 	/*
 	 * rip and regs accesses must go through
@@ -419,6 +438,8 @@ struct kvm_vcpu_arch {
 	u64 mcg_ctl;
 	u64 *mce_banks;
 
+	struct kvm_pmu pmu;
+
 	/* used for guest single stepping over the given code position */
 	unsigned long singlestep_rip;
 
@@ -870,4 +891,12 @@ extern bool kvm_find_async_pf_gfn(struct kvm_vcpu *vcpu, gfn_t gfn);
 
 void kvm_complete_insn_gp(struct kvm_vcpu *vcpu, int err);
 
+void kvm_pmu_init(struct kvm_vcpu *vcpu);
+void kvm_pmu_destroy(struct kvm_vcpu *vcpu);
+void kvm_pmu_cpuid_update(struct kvm_vcpu *vcpu);
+bool kvm_pmu_msr(struct kvm_vcpu *vcpu, u32 msr);
+int kvm_pmu_get_msr(struct kvm_vcpu *vcpu, u32 msr, u64 *data);
+int kvm_pmu_set_msr(struct kvm_vcpu *vcpu, u32 msr, u64 data);
+int kvm_pmu_read_pmc(struct kvm_vcpu *vcpu, unsigned pmc, u64 *data);
+
 #endif /* _ASM_X86_KVM_HOST_H */
arch/x86/kvm/Makefile +1 -1
index f15501f..cfca03f 100644
@@ -12,7 +12,7 @@ kvm-$(CONFIG_IOMMU_API)	+= $(addprefix ../../../virt/kvm/, iommu.o)
 kvm-$(CONFIG_KVM_ASYNC_PF)	+= $(addprefix ../../../virt/kvm/, async_pf.o)
 
 kvm-y			+= x86.o mmu.o emulate.o i8259.o irq.o lapic.o \
-			   i8254.o timer.o
+			   i8254.o timer.o pmu.o
 kvm-intel-y		+= vmx.o
 kvm-amd-y		+= svm.o
 
arch/x86/kvm/pmu.c new file +248 -0

248 lines not on this page · raw message

arch/x86/kvm/x86.c +8 -8
index 77c9d867..b3c609e 100644
@@ -593,6 +593,8 @@ static void update_cpuid(struct kvm_vcpu *vcpu)
 		if (kvm_read_cr4_bits(vcpu, X86_CR4_OSXSAVE))
 			best->ecx |= bit(X86_FEATURE_OSXSAVE);
 	}
+
+	kvm_pmu_cpuid_update(vcpu);
 }
 
 int kvm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
@@ -1561,8 +1563,6 @@ int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data)
 	 * which we perfectly emulate ;-). Any other value should be at least
 	 * reported, some guests depend on them.
 	 */
-	case MSR_P6_EVNTSEL0:
-	case MSR_P6_EVNTSEL1:
 	case MSR_K7_EVNTSEL0:
 	case MSR_K7_EVNTSEL1:
 	case MSR_K7_EVNTSEL2:
@@ -1574,8 +1574,6 @@ int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data)
 	/* at least RHEL 4 unconditionally writes to the perfctr registers,
 	 * so we ignore writes to make it happy.
 	 */
-	case MSR_P6_PERFCTR0:
-	case MSR_P6_PERFCTR1:
 	case MSR_K7_PERFCTR0:
 	case MSR_K7_PERFCTR1:
 	case MSR_K7_PERFCTR2:
@@ -1612,6 +1610,8 @@ int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data)
 	default:
 		if (msr && (msr == vcpu->kvm->arch.xen_hvm_config.msr))
 			return xen_hvm_config(vcpu, data);
+		if (kvm_pmu_msr(vcpu, msr))
+			return kvm_pmu_set_msr(vcpu, msr, data);
 		if (!ignore_msrs) {
 			pr_unimpl(vcpu, "unhandled wrmsr: 0x%x data %llx\n",
 				msr, data);
@@ -1772,10 +1772,6 @@ int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
 	case MSR_K8_SYSCFG:
 	case MSR_K7_HWCR:
 	case MSR_VM_HSAVE_PA:
-	case MSR_P6_PERFCTR0:
-	case MSR_P6_PERFCTR1:
-	case MSR_P6_EVNTSEL0:
-	case MSR_P6_EVNTSEL1:
 	case MSR_K7_EVNTSEL0:
 	case MSR_K7_PERFCTR0:
 	case MSR_K8_INT_PENDING_MSG:
@@ -1877,6 +1873,8 @@ int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
 		data = 0xbe702111;
 		break;
 	default:
+		if (kvm_pmu_msr(vcpu, msr))
+			return kvm_pmu_get_msr(vcpu, msr, pdata);
 		if (!ignore_msrs) {
 			pr_unimpl(vcpu, "unhandled rdmsr: 0x%x\n", msr);
 			return 1;
@@ -6221,6 +6219,7 @@ int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
 		goto fail_free_mce_banks;
 
 	kvm_async_pf_hash_reset(vcpu);
+	kvm_pmu_init(vcpu);
 
 	return 0;
 fail_free_mce_banks:
@@ -6239,6 +6238,7 @@ void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
 {
 	int idx;
 
+	kvm_pmu_destroy(vcpu);
 	kfree(vcpu->arch.mce_banks);
 	kvm_free_lapic(vcpu);
 	idx = srcu_read_lock(&vcpu->kvm->srcu);
Signature · 1 line
1.7.4.3
author
#1 lore raw

PATCH 1/5 perf: add context parameter to perf_event overflow handler

In reply to Avi Kivity

The perf_event overflow handler does not receive any caller-derived argument, so many callers need to resort to looking up the perf_event in their local data structure. This is ugly and doesn't scale if a single callback services many perf_events.

Fix by adding a context parameter to perf_event_create_kernel_counter() (and derived hardware breakpoints APIs) and pass it to the callback. All callers are updated.

---
 arch/arm/kernel/ptrace.c                |   10 ++++++----
 arch/powerpc/kernel/ptrace.c            |    4 ++--
 arch/sh/kernel/ptrace_32.c              |    5 +++--
 arch/x86/kernel/kgdb.c                  |    2 +-
 arch/x86/kernel/ptrace.c                |    5 +++--
 drivers/oprofile/oprofile_perf.c        |    5 +++--
 include/linux/hw_breakpoint.h           |   10 ++++++++--
 include/linux/perf_event.h              |    8 ++++++--
 kernel/events/core.c                    |   24 +++++++++++++++++-------
 kernel/events/hw_breakpoint.c           |   10 +++++++---
 kernel/watchdog.c                       |    7 +++++--
 samples/hw_breakpoint/data_breakpoint.c |    5 +++--
 12 files changed, 64 insertions(+), 31 deletions(-)
arch/arm/kernel/ptrace.c +6 -4
index 8182f45..1d6c4ae 100644
@@ -497,9 +497,10 @@ static long ptrace_hbp_idx_to_num(int idx)
 /*
  * Handle hitting a HW-breakpoint.
  */
-static void ptrace_hbptriggered(struct perf_event *bp, int unused,
-				     struct perf_sample_data *data,
-				     struct pt_regs *regs)
+static void ptrace_hbptriggered(void *context,
+				struct perf_event *bp, int unused,
+				struct perf_sample_data *data,
+				struct pt_regs *regs)
 {
 	struct arch_hw_breakpoint *bkpt = counter_arch_bp(bp);
 	long num;
@@ -580,7 +581,8 @@ static struct perf_event *ptrace_hbp_create(struct task_struct *tsk, int type)
 	attr.bp_type	= type;
 	attr.disabled	= 1;
 
-	return register_user_hw_breakpoint(&attr, ptrace_hbptriggered, tsk);
+	return register_user_hw_breakpoint(&attr, ptrace_hbptriggered, NULL,
+					   tsk);
 }
 
 static int ptrace_gethbpregs(struct task_struct *tsk, long num,
arch/powerpc/kernel/ptrace.c +2 -2
index a6ae1cf..4d42f06 100644
@@ -878,7 +878,7 @@ void user_disable_single_step(struct task_struct *task)
 }
 
 #ifdef CONFIG_HAVE_HW_BREAKPOINT
-void ptrace_triggered(struct perf_event *bp, int nmi,
+void ptrace_triggered(void *context, struct perf_event *bp, int nmi,
 		      struct perf_sample_data *data, struct pt_regs *regs)
 {
 	struct perf_event_attr attr;
@@ -969,7 +969,7 @@ int ptrace_set_debugreg(struct task_struct *task, unsigned long addr,
 								&attr.bp_type);
 
 	thread->ptrace_bps[0] = bp = register_user_hw_breakpoint(&attr,
-							ptrace_triggered, task);
+					       ptrace_triggered, NULL, task);
 	if (IS_ERR(bp)) {
 		thread->ptrace_bps[0] = NULL;
 		ptrace_put_breakpoints(task);
arch/sh/kernel/ptrace_32.c +3 -2
index 3d7b209..b08c8b0 100644
@@ -63,7 +63,7 @@ static inline int put_stack_long(struct task_struct *task, int offset,
 	return 0;
 }
 
-void ptrace_triggered(struct perf_event *bp, int nmi,
+void ptrace_triggered(void *context, struct perf_event *bp, int nmi,
 		      struct perf_sample_data *data, struct pt_regs *regs)
 {
 	struct perf_event_attr attr;
@@ -91,7 +91,8 @@ static int set_single_step(struct task_struct *tsk, unsigned long addr)
 		attr.bp_len = HW_BREAKPOINT_LEN_2;
 		attr.bp_type = HW_BREAKPOINT_R;
 
-		bp = register_user_hw_breakpoint(&attr, ptrace_triggered, tsk);
+		bp = register_user_hw_breakpoint(&attr, ptrace_triggered,
+						 NULL, tsk);
 		if (IS_ERR(bp))
 			return PTR_ERR(bp);
 
arch/x86/kernel/kgdb.c +1 -1
index 5f9ecff..473ab53 100644
@@ -638,7 +638,7 @@ void kgdb_arch_late(void)
 	for (i = 0; i < HBP_NUM; i++) {
 		if (breakinfo[i].pev)
 			continue;
-		breakinfo[i].pev = register_wide_hw_breakpoint(&attr, NULL);
+		breakinfo[i].pev = register_wide_hw_breakpoint(&attr, NULL, NULL);
 		if (IS_ERR((void * __force)breakinfo[i].pev)) {
 			printk(KERN_ERR "kgdb: Could not allocate hw"
 			       "breakpoints\nDisabling the kernel debugger\n");
arch/x86/kernel/ptrace.c +3 -2
index f65e5b5..418f5c6 100644
@@ -528,7 +528,7 @@ static int genregs_set(struct task_struct *target,
 	return ret;
 }
 
-static void ptrace_triggered(struct perf_event *bp, int nmi,
+static void ptrace_triggered(void *context, struct perf_event *bp, int nmi,
 			     struct perf_sample_data *data,
 			     struct pt_regs *regs)
 {
@@ -715,7 +715,8 @@ static int ptrace_set_breakpoint_addr(struct task_struct *tsk, int nr,
 		attr.bp_type = HW_BREAKPOINT_W;
 		attr.disabled = 1;
 
-		bp = register_user_hw_breakpoint(&attr, ptrace_triggered, tsk);
+		bp = register_user_hw_breakpoint(&attr, ptrace_triggered,
+						 NULL, tsk);
 
 		/*
 		 * CHECKME: the previous code returned -EIO if the addr wasn't
drivers/oprofile/oprofile_perf.c +3 -2
index 9046f7b..68e7f37 100644
@@ -31,7 +31,8 @@ static int num_counters;
 /*
  * Overflow callback for oprofile.
  */
-static void op_overflow_handler(struct perf_event *event, int unused,
+static void op_overflow_handler(void *context,
+			struct perf_event *event, int unused,
 			struct perf_sample_data *data, struct pt_regs *regs)
 {
 	int id;
@@ -79,7 +80,7 @@ static int op_create_counter(int cpu, int event)
 
 	pevent = perf_event_create_kernel_counter(&counter_config[event].attr,
 						  cpu, NULL,
-						  op_overflow_handler);
+						  op_overflow_handler, NULL);
 
 	if (IS_ERR(pevent))
 		return PTR_ERR(pevent);
include/linux/hw_breakpoint.h +8 -2
index d1e55fe..6ae9c63 100644
@@ -73,6 +73,7 @@ static inline unsigned long hw_breakpoint_len(struct perf_event *bp)
 extern struct perf_event *
 register_user_hw_breakpoint(struct perf_event_attr *attr,
 			    perf_overflow_handler_t triggered,
+			    void *context,
 			    struct task_struct *tsk);
 
 /* FIXME: only change from the attr, and don't unregister */
@@ -85,11 +86,13 @@ modify_user_hw_breakpoint(struct perf_event *bp, struct perf_event_attr *attr);
 extern struct perf_event *
 register_wide_hw_breakpoint_cpu(struct perf_event_attr *attr,
 				perf_overflow_handler_t	triggered,
+				void *context,
 				int cpu);
 
 extern struct perf_event * __percpu *
 register_wide_hw_breakpoint(struct perf_event_attr *attr,
-			    perf_overflow_handler_t triggered);
+			    perf_overflow_handler_t triggered,
+			    void *context);
 
 extern int register_perf_hw_breakpoint(struct perf_event *bp);
 extern int __register_perf_hw_breakpoint(struct perf_event *bp);
@@ -115,6 +118,7 @@ static inline int __init init_hw_breakpoint(void) { return 0; }
 static inline struct perf_event *
 register_user_hw_breakpoint(struct perf_event_attr *attr,
 			    perf_overflow_handler_t triggered,
+			    void *context,
 			    struct task_struct *tsk)	{ return NULL; }
 static inline int
 modify_user_hw_breakpoint(struct perf_event *bp,
@@ -122,10 +126,12 @@ modify_user_hw_breakpoint(struct perf_event *bp,
 static inline struct perf_event *
 register_wide_hw_breakpoint_cpu(struct perf_event_attr *attr,
 				perf_overflow_handler_t	 triggered,
+				void *context,
 				int cpu)		{ return NULL; }
 static inline struct perf_event * __percpu *
 register_wide_hw_breakpoint(struct perf_event_attr *attr,
-			    perf_overflow_handler_t triggered)	{ return NULL; }
+			    perf_overflow_handler_t triggered,
+			    void *context)		{ return NULL; }
 static inline int
 register_perf_hw_breakpoint(struct perf_event *bp)	{ return -ENOSYS; }
 static inline int
include/linux/perf_event.h +6 -2
index 3412684..de346a0 100644
@@ -709,7 +709,9 @@ struct perf_buffer {
 
 struct perf_sample_data;
 
-typedef void (*perf_overflow_handler_t)(struct perf_event *, int,
+typedef void (*perf_overflow_handler_t)(void *context,
+					struct perf_event *event,
+					int,
 					struct perf_sample_data *,
 					struct pt_regs *regs);
 
@@ -855,6 +857,7 @@ struct perf_event {
 	u64				id;
 
 	perf_overflow_handler_t		overflow_handler;
+	void				*overflow_handler_context;
 
 #ifdef CONFIG_EVENT_TRACING
 	struct ftrace_event_call	*tp_event;
@@ -978,7 +981,8 @@ extern struct perf_event *
 perf_event_create_kernel_counter(struct perf_event_attr *attr,
 				int cpu,
 				struct task_struct *task,
-				perf_overflow_handler_t callback);
+				perf_overflow_handler_t callback,
+				void *context);
 extern u64 perf_event_read_value(struct perf_event *event,
 				 u64 *enabled, u64 *running);
 
kernel/events/core.c +17 -7
index 0fc34a3..f60b81c 100644
@@ -5032,7 +5032,8 @@ static int __perf_event_overflow(struct perf_event *event, int nmi,
 	}
 
 	if (event->overflow_handler)
-		event->overflow_handler(event, nmi, data, regs);
+		event->overflow_handler(event->overflow_handler_context, event,
+					nmi, data, regs);
 	else
 		perf_event_output(event, nmi, data, regs);
 
@@ -6158,7 +6159,8 @@ perf_event_alloc(struct perf_event_attr *attr, int cpu,
 		 struct task_struct *task,
 		 struct perf_event *group_leader,
 		 struct perf_event *parent_event,
-		 perf_overflow_handler_t overflow_handler)
+		 perf_overflow_handler_t overflow_handler,
+		 void *context)
 {
 	struct pmu *pmu;
 	struct perf_event *event;
@@ -6216,10 +6218,13 @@ perf_event_alloc(struct perf_event_attr *attr, int cpu,
 #endif
 	}
 
-	if (!overflow_handler && parent_event)
+	if (!overflow_handler && parent_event) {
 		overflow_handler = parent_event->overflow_handler;
+		context = parent_event->overflow_handler_context;
+	}
 
 	event->overflow_handler	= overflow_handler;
+	event->overflow_handler_context = context;
 
 	if (attr->disabled)
 		event->state = PERF_EVENT_STATE_OFF;
@@ -6486,7 +6491,8 @@ SYSCALL_DEFINE5(perf_event_open,
 		}
 	}
 
-	event = perf_event_alloc(&attr, cpu, task, group_leader, NULL, NULL);
+	event = perf_event_alloc(&attr, cpu, task, group_leader, NULL,
+				 NULL, NULL);
 	if (IS_ERR(event)) {
 		err = PTR_ERR(event);
 		goto err_task;
@@ -6671,7 +6677,8 @@ err_fd:
 struct perf_event *
 perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu,
 				 struct task_struct *task,
-				 perf_overflow_handler_t overflow_handler)
+				 perf_overflow_handler_t overflow_handler,
+				 void *context)
 {
 	struct perf_event_context *ctx;
 	struct perf_event *event;
@@ -6681,7 +6688,8 @@ perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu,
 	 * Get the target context (task or percpu):
 	 */
 
-	event = perf_event_alloc(attr, cpu, task, NULL, NULL, overflow_handler);
+	event = perf_event_alloc(attr, cpu, task, NULL, NULL,
+				 overflow_handler, context);
 	if (IS_ERR(event)) {
 		err = PTR_ERR(event);
 		goto err;
@@ -6965,7 +6973,7 @@ inherit_event(struct perf_event *parent_event,
 					   parent_event->cpu,
 					   child,
 					   group_leader, parent_event,
-					   NULL);
+				           NULL, NULL);
 	if (IS_ERR(child_event))
 		return child_event;
 	get_ctx(child_ctx);
@@ -6992,6 +7000,8 @@ inherit_event(struct perf_event *parent_event,
 
 	child_event->ctx = child_ctx;
 	child_event->overflow_handler = parent_event->overflow_handler;
+	child_event->overflow_handler_context
+		= parent_event->overflow_handler_context;
 
 	/*
 	 * Precalculate sample_data sizes
kernel/events/hw_breakpoint.c +7 -3
index 086adf2..b7971d6 100644
@@ -431,9 +431,11 @@ int register_perf_hw_breakpoint(struct perf_event *bp)
 struct perf_event *
 register_user_hw_breakpoint(struct perf_event_attr *attr,
 			    perf_overflow_handler_t triggered,
+			    void *context,
 			    struct task_struct *tsk)
 {
-	return perf_event_create_kernel_counter(attr, -1, tsk, triggered);
+	return perf_event_create_kernel_counter(attr, -1, tsk, triggered,
+						context);
 }
 EXPORT_SYMBOL_GPL(register_user_hw_breakpoint);
 
@@ -502,7 +504,8 @@ EXPORT_SYMBOL_GPL(unregister_hw_breakpoint);
  */
 struct perf_event * __percpu *
 register_wide_hw_breakpoint(struct perf_event_attr *attr,
-			    perf_overflow_handler_t triggered)
+			    perf_overflow_handler_t triggered,
+			    void *context)
 {
 	struct perf_event * __percpu *cpu_events, **pevent, *bp;
 	long err;
@@ -515,7 +518,8 @@ register_wide_hw_breakpoint(struct perf_event_attr *attr,
 	get_online_cpus();
 	for_each_online_cpu(cpu) {
 		pevent = per_cpu_ptr(cpu_events, cpu);
-		bp = perf_event_create_kernel_counter(attr, cpu, NULL, triggered);
+		bp = perf_event_create_kernel_counter(attr, cpu, NULL,
+						      triggered, context);
 
 		*pevent = bp;
 
kernel/watchdog.c +5 -2
index 14733d4..e4502a9 100644
@@ -198,7 +198,8 @@ static struct perf_event_attr wd_hw_attr = {
 };
 
 /* Callback function for perf event subsystem */
-static void watchdog_overflow_callback(struct perf_event *event, int nmi,
+static void watchdog_overflow_callback(void *context,
+		 struct perf_event *event, int nmi,
 		 struct perf_sample_data *data,
 		 struct pt_regs *regs)
 {
@@ -360,7 +361,9 @@ static int watchdog_nmi_enable(int cpu)
 	/* Try to register using hardware perf events */
 	wd_attr = &wd_hw_attr;
 	wd_attr->sample_period = hw_nmi_get_sample_period();
-	event = perf_event_create_kernel_counter(wd_attr, cpu, NULL, watchdog_overflow_callback);
+	event = perf_event_create_kernel_counter(wd_attr, cpu, NULL,
+						 watchdog_overflow_callback,
+						 NULL);
 	if (!IS_ERR(event)) {
 		printk(KERN_INFO "NMI watchdog enabled, takes one hw-pmu counter.\n");
 		goto out_save;
samples/hw_breakpoint/data_breakpoint.c +3 -2
index 0636539..0635bf3 100644
@@ -41,7 +41,8 @@ module_param_string(ksym, ksym_name, KSYM_NAME_LEN, S_IRUGO);
 MODULE_PARM_DESC(ksym, "Kernel symbol to monitor; this module will report any"
 			" write operations on the kernel symbol");
 
-static void sample_hbp_handler(struct perf_event *bp, int nmi,
+static void sample_hbp_handler(void *context,
+			       struct perf_event *bp, int nmi,
 			       struct perf_sample_data *data,
 			       struct pt_regs *regs)
 {
@@ -60,7 +61,7 @@ static int __init hw_break_module_init(void)
 	attr.bp_len = HW_BREAKPOINT_LEN_4;
 	attr.bp_type = HW_BREAKPOINT_W | HW_BREAKPOINT_R;
 
-	sample_hbp = register_wide_hw_breakpoint(&attr, sample_hbp_handler);
+	sample_hbp = register_wide_hw_breakpoint(&attr, sample_hbp_handler, NULL);
 	if (IS_ERR((void __force *)sample_hbp)) {
 		ret = PTR_ERR((void __force *)sample_hbp);
 		goto fail;
Signature · 1 line
1.7.4.3
#21 lore raw
In reply to Avi Kivity
Quoted 16 lines · Avi Kivity

This not-for-merging patchset exposes an emulated version 1 architectural performance monitoring unit to KVM guests. The PMU is emulated using perf_events, so the host kernel can multiplex host-wide, host-user, and the guest on available resources.

Caveats:
- counters that have PMI (interrupt) enabled stop counting after the interrupt is signalled. This is because we need one-shot samples that keep counting, which perf doesn't support yet
- some combinations of INV and CMASK are not supported
- counters keep on counting in the host as well as the guest
- the RDPMC instruction and CR4.PCE bit are not yet emulated
- there is likely a bug in the implementation; running 'perf top' in a guest that spends 80% of its time in userspace shows perf itself as consuming almost all cpu

Quoted 5 lines

perf maintainers: please consider the first three patches for merging (the first two make sense even without the rest). If you're familiar with the Intel PMU, please review patch 5 as well - it effectively undoes all your work of abstracting the PMU into perf_events by unabstracting perf_events into what is hoped is a very similar PMU.

Gaah, I was just about to submit a talk about PMU virtualization for KVM Forum :)

Anyway, I thought about a paravirt-approach instead of implementing a real PMU... But there are certainly good reasons for both.

Joerg

#22 lore raw
Quoted 23 lines · Joerg Roedel
Quoted 16 lines · Avi Kivity

This not-for-merging patchset exposes an emulated version 1 architectural performance monitoring unit to KVM guests. The PMU is emulated using perf_events, so the host kernel can multiplex host-wide, host-user, and the guest on available resources.

Caveats:
- counters that have PMI (interrupt) enabled stop counting after the interrupt is signalled. This is because we need one-shot samples that keep counting, which perf doesn't support yet
- some combinations of INV and CMASK are not supported
- counters keep on counting in the host as well as the guest
- the RDPMC instruction and CR4.PCE bit are not yet emulated
- there is likely a bug in the implementation; running 'perf top' in a guest that spends 80% of its time in userspace shows perf itself as consuming almost all cpu

Quoted 5 lines

perf maintainers: please consider the first three patches for merging (the first two make sense even without the rest). If you're familiar with the Intel PMU, please review patch 5 as well - it effectively undoes all your work of abstracting the PMU into perf_events by unabstracting perf_events into what is hoped is a very similar PMU.

Quoted 5 lines

Gaah, I was just about to submit a talk about PMU virtualization for KVM Forum :)

Anyway, I thought about a paravirt-approach instead of implementing a real PMU... But there are certainly good reasons for both.

Paravirt is taking away the pressure from CPU vendors to do their virt extensions properly - and doesn't help with unmodifiable OSes.

Jan

Signature · 2 lines
Siemens AG, Corporate Technology, CT T DE IT 1
Corporate Competence Center Embedded Linux
author
#32 lore raw
In reply to Joerg Roedel
Quoted 17 lines · Joerg Roedel
Quoted 16 lines · Avi Kivity

This not-for-merging patchset exposes an emulated version 1 architectural performance monitoring unit to KVM guests. The PMU is emulated using perf_events, so the host kernel can multiplex host-wide, host-user, and the guest on available resources.

Caveats:
- counters that have PMI (interrupt) enabled stop counting after the interrupt is signalled. This is because we need one-shot samples that keep counting, which perf doesn't support yet
- some combinations of INV and CMASK are not supported
- counters keep on counting in the host as well as the guest
- the RDPMC instruction and CR4.PCE bit are not yet emulated
- there is likely a bug in the implementation; running 'perf top' in a guest that spends 80% of its time in userspace shows perf itself as consuming almost all cpu

Quoted 8 lines
Quoted 5 lines

perf maintainers: please consider the first three patches for merging (the first two make sense even without the rest). If you're familiar with the Intel PMU, please review patch 5 as well - it effectively undoes all your work of abstracting the PMU into perf_events by unabstracting perf_events into what is hoped is a very similar PMU.

Gaah, I was just about to submit a talk about PMU virtualization for KVM Forum :)

Speed matters.

Quoted 2 lines

Anyway, I thought about a paravirt-approach instead of implementing a real PMU... But there are certainly good reasons for both.

Note, at this time the architectural PMU is only recognized on an Intel host.

Is the statement "if an AMD processor returns non-zero information in cpuid leaf 0xa, then that processor will be compatible with other vendors' processors reporting the same information" correct?

If so, we can move the detection of the architectural pmu outside the cpu vendor checks, and this code will work on both AMD and Intel processors (even if the host cpu doesn't have an architectural PMU).

Signature · 1 line
error compiling committee.c: too many arguments to function
author
#23 lore raw
In reply to Jan Kiszka
Quoted 6 lines · Jan Kiszka
Quoted 3 lines

Anyway, I thought about a paravirt-approach instead of implementing a real PMU... But there are certainly good reasons for both.

Paravirt is taking away the pressure from CPU vendors to do their virt extensions properly - and doesn't help with unmodifiable OSes.

Yes. In the case of the PMU things are less clear, since they are very model specific. In the case of Linux, you have to lie to the guest and present a model number that it doesn't recognize, otherwise it prefers the model-specific PMU to the architectural PMU.

I though of adding a kvm cpuid bit that says "prefer the architectural pmu" to work around this issue.

Signature · 1 line
error compiling committee.c: too many arguments to function
#33 lore raw
In reply to Avi Kivity
Quoted 6 lines · Avi Kivity
Quoted 2 lines · Joerg Roedel

Gaah, I was just about to submit a talk about PMU virtualization for KVM Forum :)

Speed matters.

I'll take that as an argument for paravirt pmu, because that one is certainly faster than anything we can emulate on-top of perf_events ;-)

Quoted 6 lines

Note, at this time the architectural PMU is only recognized on an Intel host.

Is the statement "if an AMD processor returns non-zero information in cpuid leaf 0xa, then that processor will be compatible with other vendors' processors reporting the same information" correct?

AMD processors don't implement that cpuid leaf.

Quoted 3 lines

If so, we can move the detection of the architectural pmu outside the cpu vendor checks, and this code will work on both AMD and Intel processors (even if the host cpu doesn't have an architectural PMU).

Thats already some kind of paravirtualization. Don't get me wrong, I see the point of emulating a real pmu in the guest. But on the other side I think a interface that works across cpu models fits better into the KVM design, because KVM (oposed to other hypervisors) trys to hide details of the host cpu as much as necessary to get migration working between different cpus.
And since pmu are, as you said, very model-specific, some abstraction is needed. In the end probably both ways can be implemented in parallel:

* re-implementing the host-pmu using perf_events for -cpu host guests
* a paravirt pmu for everybody that wants migration and more accurate results

Regards,

Joerg

#24 lore raw
In reply to Jan Kiszka
Quoted 7 lines · Jan Kiszka
Quoted 2 lines · Joerg Roedel

Anyway, I thought about a paravirt-approach instead of implementing a real PMU... But there are certainly good reasons for both.

Paravirt is taking away the pressure from CPU vendors to do their virt extensions properly - and doesn't help with unmodifiable OSes.

Seriously, I think such decisions should be technical only and not political like that. The losers of such political decisions are always the users because they don't get useful features that are technical possible.

Regards,

Joerg

#25 lore raw
Quoted 6 lines · Joerg Roedel
Quoted 5 lines · Jan Kiszka
Quoted 2 lines · Joerg Roedel

Anyway, I thought about a paravirt-approach instead of implementing a real PMU... But there are certainly good reasons for both.

Quoted 7 lines
Quoted 2 lines

Paravirt is taking away the pressure from CPU vendors to do their virt extensions properly - and doesn't help with unmodifiable OSes.

Seriously, I think such decisions should be technical only and not political like that. The losers of such political decisions are always the users because they don't get useful features that are technical possible.

Paravirt remains a workaround, useful until hardware provides a solution for all guests, and that often in an even more efficient way (like for MMU virtualization).

We do not need to block a PV-PMU for Linux guests (or other OSes that want to adopt to it), but that will not be a solution for the problem, that's my point. A PV-PMU may even be useful to demonstrate usefulness of a virtual PMU the CPU vendors (if they aren't aware of this yet).

Jan

Signature · 2 lines
Siemens AG, Corporate Technology, CT T DE IT 1
Corporate Competence Center Embedded Linux
author
#27 lore raw
In reply to Joerg Roedel
Quoted 6 lines · Joerg Roedel
Quoted 5 lines · Jan Kiszka
Quoted 2 lines · Joerg Roedel

Anyway, I thought about a paravirt-approach instead of implementing a real PMU... But there are certainly good reasons for both.

Quoted 7 lines
Quoted 2 lines

Paravirt is taking away the pressure from CPU vendors to do their virt extensions properly - and doesn't help with unmodifiable OSes.

Seriously, I think such decisions should be technical only and not political like that. The losers of such political decisions are always the users because they don't get useful features that are technical possible.

I agree. But there are technical advantages to using architectural features instead of paravirt:

- when the cpu gains support for virtualizing the architectural feature, we transparently speed the guest up, including support for live migrating from a deployment that emulates the feature to a deployment that properly virtualizes the feature, and back. Usually the virtualized support will beat the pants off any paravirtualization we can do
- following an existing spec is a lot easier to get right than doing something from scratch
- no need to meticulously document the feature
- easier testing
- existing guest support - only need to write the host side (sometimes the only one available to us)

We saw all that with the move from shadow paging to nested paging. We had paravirt support which turned out to be poorly documented, had a security issue, and would have been slower on npt. In the end we ripped it out.

Paravirtualizing does have its advantages. For the PMU, for example, we can have a single hypercall read and reprogram all counters, saving *many* exits. But I think we need to start from the architectural PMU and see exactly what the problems are, before we optimize it to death.

Signature · 1 line
error compiling committee.c: too many arguments to function
author
#34 lore raw
In reply to Joerg Roedel
Quoted 10 lines · Joerg Roedel
Quoted 6 lines · Avi Kivity
Quoted 2 lines · Joerg Roedel

Gaah, I was just about to submit a talk about PMU virtualization for KVM Forum :)

Speed matters.

I'll take that as an argument for paravirt pmu, because that one is certainly faster than anything we can emulate on-top of perf_events ;-)

Correct, though some massaging of perf_events can make it faster. Still we'll pay with more exits with the architectural PMU.

Note a v2 PMU can reduce the exit count since it has MSRs for programming many PMCs at once.

Quoted 8 lines
Quoted 6 lines

Note, at this time the architectural PMU is only recognized on an Intel host.

Is the statement "if an AMD processor returns non-zero information in cpuid leaf 0xa, then that processor will be compatible with other vendors' processors reporting the same information" correct?

AMD processors don't implement that cpuid leaf.

Right. But if an AMD processor were to implement that leaf, it would be in a compatible manner, yes?

That allows us to

- if (vendor == intel && leaf_0xa_indicates_arch_pmu)
+ if (leaf_0xa_indicates_arch_pmu)

Quoted 4 lines
Quoted 3 lines

If so, we can move the detection of the architectural pmu outside the cpu vendor checks, and this code will work on both AMD and Intel processors (even if the host cpu doesn't have an architectural PMU).

Quoted 8 lines

Thats already some kind of paravirtualization. Don't get me wrong, I see the point of emulating a real pmu in the guest. But on the other side I think a interface that works across cpu models fits better into the KVM design, because KVM (oposed to other hypervisors) trys to hide details of the host cpu as much as necessary to get migration working between different cpus.
And since pmu are, as you said, very model-specific, some abstraction is needed.

The architectural PMU is not model specific.

Quoted 6 lines

In the end probably both ways can be implemented in parallel:

* re-implementing the host-pmu using perf_events for -cpu host guests
* a paravirt pmu for everybody that wants migration and more accurate results

A paravirt PMU also has to be implemented on top of perf_events. Otherwise we can't share this resource. So the only question is what the interface looks like. The arch pmu is non-optimized, but well specified and somewhat supported in guests. A paravirt pmu is not so well specified at this point but can be faster (less exits).

Signature · 1 line
error compiling committee.c: too many arguments to function
#26 lore raw
In reply to Jan Kiszka
Quoted 10 lines · Jan Kiszka
Quoted 4 lines · Joerg Roedel

Seriously, I think such decisions should be technical only and not political like that. The losers of such political decisions are always the users because they don't get useful features that are technical possible.

Paravirt remains a workaround, useful until hardware provides a solution for all guests, and that often in an even more efficient way (like for MMU virtualization).

Fully agreed. And todays x86 CPUs lack proper support for virtualizing the PMU. That will hopefully change but users want the feature today.

Quoted 4 lines

We do not need to block a PV-PMU for Linux guests (or other OSes that want to adopt to it), but that will not be a solution for the problem, that's my point. A PV-PMU may even be useful to demonstrate usefulness of a virtual PMU the CPU vendors (if they aren't aware of this yet).

Right, if users actually use the virtual PMU this probably increases the priority for proper hardware support.

Joerg

#28 lore raw
In reply to Avi Kivity
Quoted 9 lines · Avi Kivity

- when the cpu gains support for virtualizing the architectural feature, we transparently speed the guest up, including support for live migrating from a deployment that emulates the feature to a deployment that properly virtualizes the feature, and back. Usually the virtualized support will beat the pants off any paravirtualization we can do
- following an existing spec is a lot easier to get right than doing something from scratch
- no need to meticulously document the feature

Need to be done, but not problematic I think.

Quoted 1 line

- easier testing

Testing shouldn't be different on both variants I think.

Quoted 2 lines

- existing guest support - only need to write the host side (sometimes the only one available to us)

Otherwise I agree.

Quoted 4 lines

Paravirtualizing does have its advantages. For the PMU, for example, we can have a single hypercall read and reprogram all counters, saving *many* exits. But I think we need to start from the architectural PMU and see exactly what the problems are, before we optimize it to death.

The problem certainly is that with arch-pmu we add a lot of msr-exits to the guest-context-switch path if it uses per-task profiling. Depending on the workload this can very much distort the results.

Joerg

#35 lore raw
In reply to Avi Kivity
Quoted 6 lines · Avi Kivity
Quoted 1 line · Joerg Roedel

AMD processors don't implement that cpuid leaf.

Right. But if an AMD processor were to implement that leaf, it would be in a compatible manner, yes?

No official statement, but I guess this is the case. I have to check back, though.

Quoted 5 lines

A paravirt PMU also has to be implemented on top of perf_events. Otherwise we can't share this resource. So the only question is what the interface looks like. The arch pmu is non-optimized, but well specified and somewhat supported in guests. A paravirt pmu is not so well specified at this point but can be faster (less exits).

I agree that getting the interface right is certainly the most difficult and important task here.

Joerg

author
#29 lore raw
In reply to Joerg Roedel
Quoted 8 lines · Joerg Roedel
Quoted 4 lines

Paravirtualizing does have its advantages. For the PMU, for example, we can have a single hypercall read and reprogram all counters, saving *many* exits. But I think we need to start from the architectural PMU and see exactly what the problems are, before we optimize it to death.

The problem certainly is that with arch-pmu we add a lot of msr-exits to the guest-context-switch path if it uses per-task profiling. Depending on the workload this can very much distort the results.

Right. The combination of per-task profiling with high context switch rates is problematic.

One thing we could do is paravirtualize at a lower level, introduce a hypercall for batch MSR reads and writes. So we can use the existing PMU semantics and code, just optimize the switch. This is similar to what Xen did with lazy cpu updates, and what kvm did for paravirt pagetable writes.

I've considered something similar for mmio - use hypercalls for ordinary mmio to avoid calling into the emulator - but virtio uses pio which isn't emulated and we don't have massive consumers of mmio (except perhaps hpet).

(and we can have a cpuid bit that advertises whether we recommend to use this feature for PMU MSRs; if/when we get hardware support, we turn it off)

Signature · 1 line
error compiling committee.c: too many arguments to function
author
#30 lore raw
Quoted 2 lines · Avi Kivity

I've considered something similar for mmio - use hypercalls for ordinary mmio to avoid calling into the emulator - but virtio uses pio which isn't emulated and we don't have massive consumers of mmio (except perhaps hpet).

Say,

enum {
KVM_OP_MMIO_READ = 1,
KVM_OP_MMIO_WRITE = 2,
KVM_OP_PIO_READ = 3,
KVM_OP_PIO_WRITE = 4,
KVM_OP_MSR_READ = 5,
KVM_OP_MSR_WRITE = 6,
KVM_OP_HLT = 7, /* so we can program the apic timer and sleep */
}

struct kvm_batch_op {
    u8 op;
    u8 result;
    u8 size;
    u8 reserved[5];
    u64 address;
    u64 data;
};

int kvm_batch(int nr, u64 kvm_batch_op_phys);

This is really repeating Xen PV though (different ops), not sure it's worth the intrusiveness.

Signature · 1 line
error compiling committee.c: too many arguments to function
#31 lore raw
In reply to Avi Kivity
Quoted 7 lines · Avi Kivity

On 05/12/2011 05:24 PM, Joerg Roedel wrote:

One thing we could do is paravirtualize at a lower level, introduce a hypercall for batch MSR reads and writes. So we can use the existing PMU semantics and code, just optimize the switch. This is similar to what Xen did with lazy cpu updates, and what kvm did for paravirt pagetable writes.

In general a good idea. For MSR writes this isn't trivial as well. MSR reads and writes can cause a #GP, this needs to be represented in this interface. Can we allow that a batched MSR write changes the operation mode of the vcpu and so on.
Or we limit this interface to the PMU MSRs, then we are pretty much at a paravirt-pmu.

Quoted 8 lines

I've considered something similar for mmio - use hypercalls for ordinary mmio to avoid calling into the emulator - but virtio uses pio which isn't emulated and we don't have massive consumers of mmio (except perhaps hpet).

(and we can have a cpuid bit that advertises whether we recommend to use this feature for PMU MSRs; if/when we get hardware support, we turn it off)

A cpuid-bit that indicates which pmu is prefered is certainly a good idea.

Joerg

author
#36 lore raw
In reply to Avi Kivity
Quoted 5 lines · Avi Kivity

perf maintainers: please consider the first three patches for merging (the first two make sense even without the rest). If you're familiar with the Intel PMU, please review patch 5 as well - it effectively undoes all your work of abstracting the PMU into perf_events by unabstracting perf_events into what is hoped is a very similar PMU.

Perf maintainers, ping?

Signature · 1 line
error compiling committee.c: too many arguments to function
#8 lore raw

Re: [PATCH v1 5/5] KVM: Expose a version 1 architectural PMU to guests

In reply to Avi Kivity
Quoted 4 lines · Avi Kivity

Caveats:
- counters that have PMI (interrupt) enabled stop counting after the interrupt is signalled. This is because we need one-shot samples that keep counting, which perf doesn't support yet

Hm, do you need more than perf_event::event_limit, or something special?

Quoted 1 line

- some combinations of INV and CMASK are not supported

Could you please describe this better, where does this limit come from? If perf then this needs fixing.

Quoted 1 line

- counters keep on counting in the host as well as the guest

I suspect fixing this either needs a hw filter feature, or the ability to disable/enable these events across VM exits/entries.

I would imagine the disable/enable to be rather expensive so hw help would be preferred ...

I didnt see anything objectionable in your patches, but i'd like to have Peter's Acked-by as well before we go forward. I think that in the long run having a virtio-perf gateway would allow us a lot more tricks than just arch-perfmon emulation:

- we could do things like propagate guest side traces over to the host

- we could control from the host which events we measure on the guest side

- etc.

How would you like to handle the flow of patches - should we merge #1,#2,#3 in perf/core and you'd then merge #4,#5 via the KVM tree once the first bits hit upstream?

We could also set up a separate branch for these three commits, which you could pull - this would allow all this to still hit .40.

Thanks,

Ingo

author
#9 lore raw

Re: [PATCH v1 5/5] KVM: Expose a version 1 architectural PMU to guests

Quoted 8 lines · Ingo Molnar
Quoted 4 lines · Avi Kivity

Caveats:
- counters that have PMI (interrupt) enabled stop counting after the interrupt is signalled. This is because we need one-shot samples that keep counting, which perf doesn't support yet

Hm, do you need more than perf_event::event_limit, or something special?

I do use event_limit, but it's inexact since it auto-disables the event after firing the callback. I want to limit the callback to just one, but let the event keep counting.

Quoted 4 lines
Quoted 1 line

- some combinations of INV and CMASK are not supported

Could you please describe this better, where does this limit come from? If perf then this needs fixing.

perf_event_attr does not support generic INV and CMASK at all. I imagine you can get them through the model-specific hardware configuration, but that means we have to encode model specific information into kvm host code, which is (a) hard (b) counter to the spirit of perf.

(INV and CMASK allow you to increment the counter only when > N or < N events occur simultaneously, for example count when 2 or more instructions are retired in a single clock).

Quoted 7 lines
Quoted 1 line

- counters keep on counting in the host as well as the guest

I suspect fixing this either needs a hw filter feature, or the ability to disable/enable these events across VM exits/entries.

I would imagine the disable/enable to be rather expensive so hw help would be preferred ...

Yes, Joerg already posted support for this feature for AMD. Intel supports this in a more complicated way (you can tell vmx to load IA32_PERF_GLOBAL_CTL atomically during entry or exit). This can be done independently of this patchset.

Quoted 6 lines

I didnt see anything objectionable in your patches, but i'd like to have Peter's Acked-by as well before we go forward. I think that in the long run having a virtio-perf gateway would allow us a lot more tricks than just arch-perfmon emulation:

- we could do things like propagate guest side traces over to the host

We support that already via 'perf kvm', no? This is more about the guest profiling itself without access to the host (which is the more common scenario, IMO).

We're still missing tunnelling guest ftrace to the host, but a patch was recently posted to do that.

Quoted 10 lines

- we could control from the host which events we measure on the guest side

- etc.

How would you like to handle the flow of patches - should we merge #1,#2,#3 in perf/core and you'd then merge #4,#5 via the KVM tree once the first bits hit upstream?

We could also set up a separate branch for these three commits, which you could pull - this would allow all this to still hit .40.

You can put them in either perf/core or a different branch, and I can pull them as long as its fast-forward only.

But I don't think I'll target 2.6.40, this needs to be tested a lot more, particularly wrt guest-initiated DoS against the host.

Signature · 1 line
error compiling committee.c: too many arguments to function
#10 lore raw

Re: [PATCH v1 5/5] KVM: Expose a version 1 architectural PMU to guests

Quoted 6 lines · Avi Kivity
Quoted 4 lines
Quoted 1 line

- some combinations of INV and CMASK are not supported

Could you please describe this better, where does this limit come from? If perf then this needs fixing.

perf_event_attr does not support generic INV and CMASK at all. [...]

It does through raw events - which are indeed model specific.

Quoted 7 lines

[...] I imagine you can get them through the model-specific hardware configuration, but that means we have to encode model specific information into kvm host code, which is (a) hard (b) counter to the spirit of perf.

(INV and CMASK allow you to increment the counter only when > N or < N events occur simultaneously, for example count when 2 or more instructions are retired in a single clock).

Peter, what do you think about adding a inv and cmask attribute that is applied to generic events automatically?

I *think* it should work just out of box: we recognize the fixed-purpose counter events based on their raw value, so if one comes in with inv and cmask set it will just be scheduled on a generic hw counter.

Quoted 3 lines
Quoted 1 line

- we could do things like propagate guest side traces over to the host

We support that already via 'perf kvm', no? [...]

Not without usability issues, if you remember that flamewa^W thread! :-)

Quoted 2 lines

[...] This is more about the guest profiling itself without access to the host (which is the more common scenario, IMO).

Correct - so there would be interactions with virtio-perf. I think the two will just have to be made to mix well, that's all.

Quoted 2 lines

We're still missing tunnelling guest ftrace to the host, but a patch was recently posted to do that.

If you tunnel guest perf events then ftrace events are included automatically. That's the primary/unified event transport model we are working towards.

Thanks,

Ingo

#11 lore raw

Re: [PATCH v1 5/5] KVM: Expose a version 1 architectural PMU to guests

Quoted 3 lines · Ingo Molnar

Peter, what do you think about adding a inv and cmask attribute that is applied to generic events automatically?

Have you read the PPC, ARM and SPARC64 PMU definition to check if that makes sense?

#13 lore raw

Re: [PATCH v1 5/5] KVM: Expose a version 1 architectural PMU to guests

In reply to Ingo Molnar
Quoted 2 lines · Ingo Molnar

It does through raw events - which are indeed model specific.

Which is exactly what is needed anyway, he gets a raw msr value.

The only thing that is not exposed is the ANY bit, but since KVM doesn't expose HT anyway that doesn't matter.

author
#14 lore raw

Re: [PATCH v1 5/5] KVM: Expose a version 1 architectural PMU to guests

Quoted 8 lines · Peter Zijlstra
Quoted 2 lines · Ingo Molnar

It does through raw events - which are indeed model specific.

Which is exactly what is needed anyway, he gets a raw msr value.

The only thing that is not exposed is the ANY bit, but since KVM doesn't expose HT anyway that doesn't matter.

If I were to use raw events, I'd need to program AMD and Intel hosts separately. As it is, I just use the generic counters and the perf backend does its thing.

(note, INV and CMASK are hardly important, I think we can get away without implementing them)

Signature · 1 line
error compiling committee.c: too many arguments to function
#12 lore raw

Re: [PATCH v1 5/5] KVM: Expose a version 1 architectural PMU to guests

In reply to Peter Zijlstra
Quoted 7 lines · Peter Zijlstra
Quoted 3 lines · Ingo Molnar

Peter, what do you think about adding a inv and cmask attribute that is applied to generic events automatically?

Have you read the PPC, ARM and SPARC64 PMU definition to check if that makes sense?

No - some feedback from them would be nice.

Thanks,

Ingo

#15 lore raw

Re: [PATCH v1 5/5] KVM: Expose a version 1 architectural PMU to guests

In reply to Avi Kivity
Quoted 5 lines · Avi Kivity
Quoted 4 lines · Peter Zijlstra
Quoted 2 lines · Ingo Molnar

It does through raw events - which are indeed model specific.

Quoted 8 lines
Quoted 4 lines

Which is exactly what is needed anyway, he gets a raw msr value.

The only thing that is not exposed is the ANY bit, but since KVM doesn't expose HT anyway that doesn't matter.

If I were to use raw events, I'd need to program AMD and Intel hosts separately. As it is, I just use the generic counters and the perf backend does its thing.

But why exactly, from what I understood you emulate an actual hardware PMU, that means the guest will be writing proper content to the relevant MSRs, you can feed that directly into the raw config field (with exception of the USR/OS/INT bits).

I'm fairly sure that emulating the intel arch bits on an cpu that otherwise identifies itself as AMD is going to confuse things.

author
#16 lore raw

Re: [PATCH v1 5/5] KVM: Expose a version 1 architectural PMU to guests

Quoted 11 lines · Peter Zijlstra
Quoted 10 lines · Avi Kivity
Quoted 8 lines · Peter Zijlstra
Quoted 2 lines · Ingo Molnar

It does through raw events - which are indeed model specific.

Which is exactly what is needed anyway, he gets a raw msr value.

The only thing that is not exposed is the ANY bit, but since KVM doesn't expose HT anyway that doesn't matter.

Quoted 8 lines
Quoted 3 lines

If I were to use raw events, I'd need to program AMD and Intel hosts separately. As it is, I just use the generic counters and the perf backend does its thing.

But why exactly, from what I understood you emulate an actual hardware PMU, that means the guest will be writing proper content to the relevant MSRs, you can feed that directly into the raw config field (with exception of the USR/OS/INT bits).

I am emulating an architectural PMU on a machine that may not have one (Intel P4 or AMD), so I need translation between the different config formats and event codes.

Quoted 2 lines

I'm fairly sure that emulating the intel arch bits on an cpu that otherwise identifies itself as AMD is going to confuse things.

First, we can cheat and identify as Intel. Second, I'd like to change architectural PMU probing to ignore the vendor (pending confirmation from AMD that they won't implement cpuid leaf 0xa in a way that is incompatible with Intel). That's what architectural means, no?

Signature · 1 line
error compiling committee.c: too many arguments to function
author
#37 lore raw
In reply to Avi Kivity
Quoted 9 lines · Avi Kivity
Quoted 5 lines · Avi Kivity

perf maintainers: please consider the first three patches for merging (the first two make sense even without the rest). If you're familiar with the Intel PMU, please review patch 5 as well - it effectively undoes all your work of abstracting the PMU into perf_events by unabstracting perf_events into what is hoped is a very similar PMU.

Perf maintainers, ping?

Re-ping?

Signature · 1 line
error compiling committee.c: too many arguments to function
#38 lore raw
Quoted 11 lines · Avi Kivity
Quoted 10 lines · Avi Kivity
Quoted 8 lines · Avi Kivity

perf maintainers: please consider the first three patches for merging (the
first two make sense even without the rest). If you're familiar with the Intel
PMU, please review patch 5 as well - it effectively undoes all your work of abstracting the PMU into perf_events by unabstracting perf_events into what
is hoped is a very similar PMU.

Quoted 4 lines
Quoted 2 lines

Perf maintainers, ping?

Re-ping?

Still struggling with merge window fallout, this is soon after that.

author
#39 lore raw
Quoted 4 lines · Peter Zijlstra
Quoted 2 lines

Re-ping?

Still struggling with merge window fallout, this is soon after that.

Thanks.

Signature · 1 line
error compiling committee.c: too many arguments to function
#2 lore raw

Re: [PATCH v1 1/5] perf: add context parameter to perf_event overflow handler

In reply to Avi Kivity
Quoted 13 lines · Avi Kivity

+++ b/include/linux/perf_event.h

include/linux/perf_event.h +3 -1
@@ -709,7 +709,9 @@ struct perf_buffer {
 
 struct perf_sample_data;
 
-typedef void (*perf_overflow_handler_t)(struct perf_event *, int,
+typedef void (*perf_overflow_handler_t)(void *context,
+                                       struct perf_event *event,
+                                       int,
                                        struct perf_sample_data *,
                                        struct pt_regs *regs);
 

include/linux/perf_event.h struct perf_event { · line 863

Quoted 7 lines
include/linux/perf_event.h +1 -0
@@ -855,6 +857,7 @@ struct perf_event {
        u64                             id;
 
        perf_overflow_handler_t         overflow_handler;
+       void                            *overflow_handler_context;
 
 #ifdef CONFIG_EVENT_TRACING
        struct ftrace_event_call        *tp_event; 

If you're adding a pointer here, then why pass it along in the callback thing if it already has the perf_event *?

It seems to me the callback can easily do event->overflow_handler_context if it needs the thing.

#17 lore raw

Re: [PATCH v1 5/5] KVM: Expose a version 1 architectural PMU to guests

In reply to Avi Kivity
Quoted 3 lines · Avi Kivity

- counters that have PMI (interrupt) enabled stop counting after the interrupt is signalled. This is because we need one-shot samples that keep counting, which perf doesn't support yet

You'll have to reprogram the thing anyway, since not all hardware has the same counter width:

[    0.046996] Performance Events: AMD PMU driver.
[    0.048998] ... bit width:              48

vs

[    0.026998] Performance Events: PEBS fmt0+, Core2 events, Intel PMU driver.
[    0.026998] ... bit width:              40

simply letting the thing run will not behave in a consistent fashion.

Or are you going to assume all software will properly read the cpuid leaf and not assume bit width?

Also, I can't seem to locate where you fill that cpuid-leaf, kvm_pmu_cpuid_update() seems to read the entry, not write it.

#19 lore raw

Re: [PATCH v1 5/5] KVM: Expose a version 1 architectural PMU to guests

In reply to Avi Kivity

arch/x86/kvm/pmu.c line 74

Quoted 11 lines · Avi Kivity
arch/x86/kvm/pmu.c new file +11 -0
+static u64 read_gp_pmc(struct kvm_pmu *pmu, struct kvm_pmc *pmc)
+{
+       u64 counter, enabled, running;
+
+       counter = pmc->counter;
+
+       if (pmc->perf_event)
+               counter += perf_event_read_value(pmc->perf_event,
+                                                &enabled, &running);
+
+       /* FIXME: Scaling needed? */

Yeah, either that or use attr.pinned = 1, doing the latter will result in an error when the PMU is overloaded though.

Quoted 2 lines
(quoted diff) +2 -0
+       return counter & pmu->counter_bitmask;
+} 
author
#3 lore raw

Re: [PATCH v1 1/5] perf: add context parameter to perf_event overflow handler

In reply to Peter Zijlstra
Quoted 19 lines · Peter Zijlstra
Quoted 18 lines · Avi Kivity

+++ b/include/linux/perf_event.h
@@ -709,7 +709,9 @@ struct perf_buffer {

struct perf_sample_data;

-typedef void (*perf_overflow_handler_t)(struct perf_event *, int, +typedef void (*perf_overflow_handler_t)(void *context,
+ struct perf_event *event,
+ int,
struct perf_sample_data *, struct pt_regs *regs);

 @@ -855,6 +857,7 @@ struct perf_event {
         u64                             id;

         perf_overflow_handler_t         overflow_handler;

+ void *overflow_handler_context;

Quoted 8 lines
Quoted 2 lines
  #ifdef CONFIG_EVENT_TRACING
         struct ftrace_event_call        *tp_event;

If you're adding a pointer here, then why pass it along in the callback thing if it already has the perf_event *?

It seems to me the callback can easily do event->overflow_handler_context if it needs the thing.

Makes sense. Will update.

(I would have preferred a caller-allocated perf_event with container_of(), but that's a huge change).

Signature · 1 line
error compiling committee.c: too many arguments to function
author
#18 lore raw

Re: [PATCH v1 5/5] KVM: Expose a version 1 architectural PMU to guests

In reply to Peter Zijlstra
Quoted 11 lines · Peter Zijlstra
Quoted 3 lines · Avi Kivity

- counters that have PMI (interrupt) enabled stop counting after the interrupt is signalled. This is because we need one-shot samples that keep counting, which perf doesn't support yet

You'll have to reprogram the thing anyway, since not all hardware has the same counter width:

[    0.046996] Performance Events: AMD PMU driver.
[    0.048998] ... bit width:              48
Quoted 6 lines

vs

[    0.026998] Performance Events: PEBS fmt0+, Core2 events, Intel PMU driver.
[    0.026998] ... bit width:              40

simply letting the thing run will not behave in a consistent fashion.

I don't really follow. How is the bit width related?

We adjust for bit width during PMC read.

Though I agree for accurate emulation we do need to reprogram, so we can set the next overflow event at 2^bit_width. I doubt that anyone relies on this second overflow (even with 40 bits counting cycles, it takes far too long to overflow), still we have to emulate it.

Quoted 5 lines

Or are you going to assume all software will properly read the cpuid leaf and not assume bit width?

Also, I can't seem to locate where you fill that cpuid-leaf, kvm_pmu_cpuid_update() seems to read the entry, not write it.

We rely on host userspace to set up cpuid (and KVM_GET_SUPPORTED_CPUID to report to userspace what we support).

Signature · 1 line
error compiling committee.c: too many arguments to function
author
#20 lore raw

Re: [PATCH v1 5/5] KVM: Expose a version 1 architectural PMU to guests

In reply to Peter Zijlstra
Quoted 13 lines · Peter Zijlstra
Quoted 11 lines · Avi Kivity

+static u64 read_gp_pmc(struct kvm_pmu *pmu, struct kvm_pmc *pmc) +{
+ u64 counter, enabled, running;
+
+ counter = pmc->counter;
+
+ if (pmc->perf_event)
+ counter += perf_event_read_value(pmc->perf_event,
+&enabled,&running);
+
+ /* FIXME: Scaling needed? */

Quoted 2 lines

Yeah, either that or use attr.pinned = 1, doing the latter will result in an error when the PMU is overloaded though.

So what's the scale factor? enabled / running?

Signature · 1 line
error compiling committee.c: too many arguments to function