Return-Path: Received: from mail.linutronix.de (146.0.238.70:993) by crypto-ml.lab.linutronix.de with IMAP4-SSL for ; 06 Feb 2019 19:07:36 -0000 Received: from mga17.intel.com ([192.55.52.151]) by Galois.linutronix.de with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1grSXm-0003SK-RR for speck@linutronix.de; Wed, 06 Feb 2019 20:07:35 +0100 Date: Wed, 6 Feb 2019 11:07:30 -0800 From: Andi Kleen Subject: [MODERATED] Re: [PATCH v2 3/8] PERFv2 Message-ID: <20190206190730.GN31598@tassilo.jf.intel.com> References: <583e51cd2a2246421610871ee7413907345f7828.1549416008.git.ak@linux.intel.com> MIME-Version: 1.0 In-Reply-To: <583e51cd2a2246421610871ee7413907345f7828.1549416008.git.ak@linux.intel.com> Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit To: speck@linutronix.de List-ID: I managed to post a old version of the patch. Please use this one. Only two lines difference, so no full repost. On Skylake with recent microcode updates due to errata XXX perfmon general purpose counter 3 can be corrupted when RTM transactions are executed. The microcode provides a new MSR to force disable RTM (make all RTM transactions abort). This patch adds the low level code to manage this MSR. Depending on a per event flag TSX is disabled while a perf event that uses counter 3 is running. When the event flag is not set, then avoid using counter 3. When the event is scheduled out again reenable TSX again. This patch avoids reprograming the MSR when counter 3 is reused for a different MSR. Future patches will add the user interfaces to enable this functionality. Signed-off-by: Andi Kleen --- v2: Use u8 instead of bool Rename force_rtm_abort_active. v3: Use correct patch version that actually compiles. diff --git a/arch/x86/events/intel/core.c b/arch/x86/events/intel/core.c index daafb893449b..6d3b7ec2888b 100644 --- a/arch/x86/events/intel/core.c +++ b/arch/x86/events/intel/core.c @@ -2042,6 +2042,16 @@ static inline bool event_is_checkpointed(struct perf_event *event) return (event->hw.config & HSW_IN_TX_CHECKPOINTED) != 0; } +static void intel_pmu_disable_fra(struct perf_event *event) +{ + struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); + + if (event->hw.idx == 3 && cpuc->rtm_abort_set) { + wrmsrl(MSR_TSX_FORCE_ABORT, 0); + cpuc->rtm_abort_set = false; + } +} + static void intel_pmu_disable_event(struct perf_event *event) { struct hw_perf_event *hwc = &event->hw; @@ -2060,6 +2070,9 @@ static void intel_pmu_disable_event(struct perf_event *event) if (unlikely(event->attr.precise_ip)) intel_pmu_pebs_disable(event); + if (static_cpu_has(X86_FEATURE_TSX_FORCE_ABORT)) + intel_pmu_disable_fra(event); + if (unlikely(hwc->config_base == MSR_ARCH_PERFMON_FIXED_CTR_CTRL)) { intel_pmu_disable_fixed(hwc); return; @@ -2084,6 +2097,30 @@ static void intel_pmu_read_event(struct perf_event *event) x86_perf_event_update(event); } +static bool __use_counter3(struct perf_event *event) +{ + return event->hw.flags & PERF_X86_EVENT_ABORT_TSX; +} + +static bool use_counter3(struct perf_event *event) +{ + return __use_counter3(event) || + (event->group_leader && __use_counter3(event->group_leader)); +} + +static void intel_pmu_enable_fra(struct perf_event *event) +{ + struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); + + if (use_counter3(event) && + event->hw.idx == 3 && + !cpuc->rtm_abort_set) { + /* No RMW, so other bits get clobbered */ + wrmsrl(MSR_TSX_FORCE_ABORT, MSR_TFA_RTM_FORCE_ABORT); + cpuc->rtm_abort_set = true; + } +} + static void intel_pmu_enable_fixed(struct perf_event *event) { struct hw_perf_event *hwc = &event->hw; @@ -2141,6 +2178,9 @@ static void intel_pmu_enable_event(struct perf_event *event) if (unlikely(event->attr.precise_ip)) intel_pmu_pebs_enable(event); + if (static_cpu_has(X86_FEATURE_TSX_FORCE_ABORT)) + intel_pmu_enable_fra(event); + if (unlikely(hwc->config_base == MSR_ARCH_PERFMON_FIXED_CTR_CTRL)) { intel_pmu_enable_fixed(event); return; diff --git a/arch/x86/events/perf_event.h b/arch/x86/events/perf_event.h index 5b08ae3edafd..2c2397035591 100644 --- a/arch/x86/events/perf_event.h +++ b/arch/x86/events/perf_event.h @@ -70,7 +70,7 @@ struct event_constraint { #define PERF_X86_EVENT_EXCL_ACCT 0x0200 /* accounted EXCL event */ #define PERF_X86_EVENT_AUTO_RELOAD 0x0400 /* use PEBS auto-reload */ #define PERF_X86_EVENT_LARGE_PEBS 0x0800 /* use large PEBS */ - +#define PERF_X86_EVENT_ABORT_TSX 0x1000 /* force abort TSX */ struct amd_nb { int nb_id; /* NorthBridge id */ @@ -242,6 +242,11 @@ struct cpu_hw_events { struct intel_excl_cntrs *excl_cntrs; int excl_thread_id; /* 0 or 1 */ + /* + * Manage using counter 3 on Skylake with TSX. + */ + u8 rtm_abort_set; /* We set the MSR */ + /* * 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..d7f29ca27ca1 100644 --- a/arch/x86/include/asm/msr-index.h +++ b/arch/x86/include/asm/msr-index.h @@ -666,6 +666,9 @@ #define MSR_IA32_TSC_DEADLINE 0x000006E0 +#define MSR_TSX_FORCE_ABORT 0x0000010F +#define MSR_TFA_RTM_FORCE_ABORT BIT_ULL(0) + /* P4/Xeon+ specific */ #define MSR_IA32_MCG_EAX 0x00000180 #define MSR_IA32_MCG_EBX 0x00000181