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:33:54 -0000 Received: from merlin.infradead.org ([2001:8b0:10b:1231::1]) by Galois.linutronix.de with esmtps (TLS1.2:RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1grjoO-00024b-Ji for speck@linutronix.de; Thu, 07 Feb 2019 14:33:53 +0100 Received: from j217100.upc-j.chello.nl ([24.132.217.100] helo=hirez.programming.kicks-ass.net) by merlin.infradead.org with esmtpsa (Exim 4.90_1 #2 (Red Hat Linux)) id 1grjoN-0004qo-7U for speck@linutronix.de; Thu, 07 Feb 2019 13:33:51 +0000 Date: Thu, 7 Feb 2019 14:33:48 +0100 From: Peter Zijlstra Subject: [MODERATED] Re: [PATCH v2 0/8] PERFv2 Message-ID: <20190207133348.GA32534@hirez.programming.kicks-ass.net> References: <20190207132709.GA32477@hirez.programming.kicks-ass.net> MIME-Version: 1.0 In-Reply-To: <20190207132709.GA32477@hirez.programming.kicks-ass.net> Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit To: speck@linutronix.de List-ID: On Thu, Feb 07, 2019 at 02:27:09PM +0100, Peter Zijlstra wrote: > 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 s/yet/yes/, obviously. > simplified. Like so.. --- arch/x86/events/intel/core.c | 60 +++++++++++++++++++++++++++++++++++++- 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, 71 insertions(+), 1 deletion(-) diff --git a/arch/x86/events/intel/core.c b/arch/x86/events/intel/core.c index e0232bdb7aff..11f96a331967 100644 --- a/arch/x86/events/intel/core.c +++ b/arch/x86/events/intel/core.c @@ -1999,6 +1999,31 @@ 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); + u64 val; + + /* + * Since we have GLOBAL_CTRL.3 == 0, and have not used RTM + * instructions, PMC3 should be 'stable'; IOW the values we + * just potentially programmed into it, should still be there. + * + * If we programmed PMC3, make sure to set TFA before we make + * things go and possibly encounter RTM instructions. + * Similarly, if PMC3 got unused, make sure to clear TFA. + */ + val = MSR_TFA_RTM_FORCE_ABORT * test_bit(3, cpuc->active_mask); + if (cpuc->tfa_shadow != val) { + cpuc->tfa_shadow = val; + wrmsrl(MSR_TSX_FORCE_ABORT, val); + } + } + + intel_pmu_enable_all(added); +} + static void enable_counter_freeze(void) { update_debugctlmsr(get_debugctlmsr() | @@ -3345,6 +3370,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 +4112,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 +4591,7 @@ __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.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 +4601,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