Return-Path: Received: from mail.linutronix.de (146.0.238.70:993) by crypto-ml.lab.linutronix.de with IMAP4-SSL for ; 07 Feb 2019 13:27:17 -0000 Received: from bombadil.infradead.org ([2607:7c80:54:e::133]) by Galois.linutronix.de with esmtps (TLS1.2:RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1grjhz-0001s8-Ev for speck@linutronix.de; Thu, 07 Feb 2019 14:27:15 +0100 Received: from j217100.upc-j.chello.nl ([24.132.217.100] helo=hirez.programming.kicks-ass.net) by bombadil.infradead.org with esmtpsa (Exim 4.90_1 #2 (Red Hat Linux)) id 1grjhw-0000dQ-7d for speck@linutronix.de; Thu, 07 Feb 2019 13:27:12 +0000 Date: Thu, 7 Feb 2019 14:27:09 +0100 From: Peter Zijlstra Subject: [MODERATED] Re: [PATCH v2 0/8] PERFv2 Message-ID: <20190207132709.GA32477@hirez.programming.kicks-ass.net> References: MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit To: speck@linutronix.de List-ID: On Tue, Feb 05, 2019 at 05:23:54PM -0800, speck for Andi Kleen wrote: > Andi Kleen (8): > x86/pmu/intel: Export number of counters in caps > x86/pmu/intel: Support excluding GP counters in scheduler > x86/pmu/intel: Handle TSX with counter 3 on Skylake > x86/pmu/intel: Add perf event attribute to enable counter 3 > x86/pmu/intel: Add global option for enable all counters > perf stat: Make all existing groups weak > perf stat: Don't count EL for --transaction with three counters > kvm: vmx: Support TSX_FORCE_ABORT in KVM guests > > arch/x86/events/core.c | 50 ++++++++++++++++++--- > arch/x86/events/intel/core.c | 72 ++++++++++++++++++++++++++++++ > arch/x86/events/intel/uncore.c | 2 +- > arch/x86/events/perf_event.h | 15 ++++++- > arch/x86/include/asm/cpufeatures.h | 1 + > arch/x86/include/asm/kvm_host.h | 1 + > arch/x86/include/asm/msr-index.h | 3 ++ > arch/x86/kvm/cpuid.c | 3 +- > arch/x86/kvm/pmu.c | 19 +++++--- > arch/x86/kvm/pmu.h | 6 ++- > arch/x86/kvm/pmu_amd.c | 2 +- > arch/x86/kvm/vmx/pmu_intel.c | 16 ++++++- > include/linux/perf_event.h | 1 + > tools/perf/builtin-stat.c | 38 +++++++++++----- > tools/perf/util/pmu.c | 10 +++++ > tools/perf/util/pmu.h | 1 + > 16 files changed, 206 insertions(+), 34 deletions(-) A few questions: - How will this 'feature' interact with MSR_CORE_PERF_GLOBAL_CTRL ? - Is having GLOBAL_CTRL.3 0 and not using RTM instructions enough guarantee to avoid PMC3 from being clobbered? Esp. that latter question, because both your patches and the below seems to rely on that; and if the answer is yet, the below can be further simplified. So what (aside from being _completely_ untested and not doing virt crap) is wrong with the below? It sure as heck is simpler. --- arch/x86/events/intel/core.c | 71 +++++++++++++++++++++++++++++++++++++- arch/x86/events/perf_event.h | 5 +++ arch/x86/include/asm/cpufeatures.h | 1 + arch/x86/include/asm/msr-index.h | 6 ++++ 4 files changed, 82 insertions(+), 1 deletion(-) diff --git a/arch/x86/events/intel/core.c b/arch/x86/events/intel/core.c index e0232bdb7aff..8e63c34409c2 100644 --- a/arch/x86/events/intel/core.c +++ b/arch/x86/events/intel/core.c @@ -1999,6 +1999,23 @@ static void intel_pmu_nhm_enable_all(int added) intel_pmu_enable_all(added); } +static void intel_skl_pmu_enable_all(int added) +{ + if (static_cpu_has(X86_FEATURE_TSX_FORCE_ABORT)) { + struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); + + /* + * When, after programming the PMU, PMC3 is unused; clear TFA. + */ + if (!test_bit(3, cpuc->active_mask) && cpuc->tfa_shadow) { + wrmsrl(MSR_TSX_FORCE_ABORT, 0); + cpuc->tfa_shadow = 0; + } + } + + intel_pmu_enable_all(added); +} + static void enable_counter_freeze(void) { update_debugctlmsr(get_debugctlmsr() | @@ -2149,6 +2166,24 @@ static void intel_pmu_enable_event(struct perf_event *event) __x86_pmu_enable_event(hwc, ARCH_PERFMON_EVENTSEL_ENABLE); } +static void intel_skl_enable_event(struct perf_event *event) +{ + if (static_cpu_has(X86_FEATURE_TSX_FORCE_ABORT)) { + struct hw_perf_event *hwc = &event->hw; + struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); + + /* + * We must set TFA before writing to the PMC3 registers. + */ + if (hwc->idx == 3 && !cpuc->tfa_shadow) { + wrmsrl(MSR_TSX_FORCE_ABORT, MSR_TFA_RTM_FORCE_ABORT); + cpuc->tfa_shadow = 1; + } + } + + intel_pmu_enable_event(event); +} + static void intel_pmu_add_event(struct perf_event *event) { if (event->attr.precise_ip) @@ -3345,6 +3380,32 @@ glp_get_event_constraints(struct cpu_hw_events *cpuc, int idx, return c; } +static bool force_rtm_abort = true; + +static struct event_constraint * +skl_get_event_constraints(struct cpu_hw_events *cpuc, int idx, + struct perf_event *event) +{ + struct event_constraint *c; + + c = hsw_get_event_constraints(cpuc, idx, event); + + if (!force_rtm_abort) { + /* + * Without TFA we must not use PMC3. + */ + c->idxmsk64 &= ~(1ULL << 3); + } else { + /* + * Restore PMC3 to match PMC2; there is no SKL constraint that + * has PMC2 but not PMC3. + */ + c->idxmsk64 |= (c->idxmsk64 & (1ULL << 2)) << 1; + } + + return c; +} + /* * Broadwell: * @@ -4061,9 +4122,12 @@ static struct attribute *intel_pmu_caps_attrs[] = { NULL }; +DEVICE_BOOL_ATTR(force_rtm_abort, 0644, force_rtm_abort); + static struct attribute *intel_pmu_attrs[] = { &dev_attr_freeze_on_smi.attr, NULL, + NULL, }; static __init struct attribute ** @@ -4537,6 +4601,8 @@ __init int intel_pmu_init(void) event_attr_td_recovery_bubbles.event_str_ht = "event=0xd,umask=0x1,cmask=1,any=1"; + x86_pmu.enable_all = intel_skl_pmu_enable_all; + x86_pmu.enable = intel_skl_enable_event; x86_pmu.event_constraints = intel_skl_event_constraints; x86_pmu.pebs_constraints = intel_skl_pebs_event_constraints; x86_pmu.extra_regs = intel_skl_extra_regs; @@ -4546,8 +4612,11 @@ __init int intel_pmu_init(void) x86_pmu.flags |= PMU_FL_HAS_RSP_1; x86_pmu.flags |= PMU_FL_NO_HT_SHARING; + if (boot_cpu_has(X86_FEATURE_TSX_FORCE_ABORT)) + intel_pmu_attrs[1] = &dev_attr_force_rtm_abort.attr.attr; + x86_pmu.hw_config = hsw_hw_config; - x86_pmu.get_event_constraints = hsw_get_event_constraints; + x86_pmu.get_event_constraints = skl_get_event_constraints; extra_attr = boot_cpu_has(X86_FEATURE_RTM) ? hsw_format_attr : nhm_format_attr; extra_attr = merge_attr(extra_attr, skl_format_attr); diff --git a/arch/x86/events/perf_event.h b/arch/x86/events/perf_event.h index 78d7b7031bfc..0ced0d9a1dde 100644 --- a/arch/x86/events/perf_event.h +++ b/arch/x86/events/perf_event.h @@ -242,6 +242,11 @@ struct cpu_hw_events { struct intel_excl_cntrs *excl_cntrs; int excl_thread_id; /* 0 or 1 */ + /* + * SKL TSX_FORCE_ABORT shadow + */ + int tfa_shadow; + /* * AMD specific bits */ diff --git a/arch/x86/include/asm/cpufeatures.h b/arch/x86/include/asm/cpufeatures.h index 6d6122524711..981ff9479648 100644 --- a/arch/x86/include/asm/cpufeatures.h +++ b/arch/x86/include/asm/cpufeatures.h @@ -344,6 +344,7 @@ /* Intel-defined CPU features, CPUID level 0x00000007:0 (EDX), word 18 */ #define X86_FEATURE_AVX512_4VNNIW (18*32+ 2) /* AVX-512 Neural Network Instructions */ #define X86_FEATURE_AVX512_4FMAPS (18*32+ 3) /* AVX-512 Multiply Accumulation Single precision */ +#define X86_FEATURE_TSX_FORCE_ABORT (18*32+13) /* "" TSX_FORCE_ABORT */ #define X86_FEATURE_PCONFIG (18*32+18) /* Intel PCONFIG */ #define X86_FEATURE_SPEC_CTRL (18*32+26) /* "" Speculation Control (IBRS + IBPB) */ #define X86_FEATURE_INTEL_STIBP (18*32+27) /* "" Single Thread Indirect Branch Predictors */ diff --git a/arch/x86/include/asm/msr-index.h b/arch/x86/include/asm/msr-index.h index 8e40c2446fd1..ca5bc0eacb95 100644 --- a/arch/x86/include/asm/msr-index.h +++ b/arch/x86/include/asm/msr-index.h @@ -666,6 +666,12 @@ #define MSR_IA32_TSC_DEADLINE 0x000006E0 + +#define MSR_TSX_FORCE_ABORT 0x0000010F + +#define MSR_TFA_RTM_FORCE_ABORT_BIT 0 +#define MSR_TFA_RTM_FORCE_ABORT BIT_ULL(MSR_TFA_RTM_FORCE_ABORT_BIT) + /* P4/Xeon+ specific */ #define MSR_IA32_MCG_EAX 0x00000180 #define MSR_IA32_MCG_EBX 0x00000181