Skip to content

Commit 26b6e01

Browse files
Peter Zijlstra (Intel)gregkh
authored andcommitted
perf/x86/intel: Implement support for TSX Force Abort
commit 400816f upstream Skylake (and later) will receive a microcode update to address a TSX errata. This microcode will, on execution of a TSX instruction (speculative or not) use (clobber) PMC3. This update will also provide a new MSR to change this behaviour along with a CPUID bit to enumerate the presence of this new MSR. When the MSR gets set; the microcode will no longer use PMC3 but will Force Abort every TSX transaction (upon executing COMMIT). When TSX Force Abort (TFA) is allowed (default); the MSR gets set when PMC3 gets scheduled and cleared when, after scheduling, PMC3 is unused. When TFA is not allowed; clear PMC3 from all constraints such that it will not get used. Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Signed-off-by: Thomas Gleixner <tglx@linutronix.de> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent fdd8209 commit 26b6e01

2 files changed

Lines changed: 77 additions & 3 deletions

File tree

‎arch/x86/events/intel/core.c‎

Lines changed: 71 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1995,6 +1995,39 @@ static void intel_pmu_nhm_enable_all(int added)
19951995
intel_pmu_enable_all(added);
19961996
}
19971997

1998+
static void intel_set_tfa(struct cpu_hw_events *cpuc, bool on)
1999+
{
2000+
u64 val = on ? MSR_TFA_RTM_FORCE_ABORT : 0;
2001+
2002+
if (cpuc->tfa_shadow != val) {
2003+
cpuc->tfa_shadow = val;
2004+
wrmsrl(MSR_TSX_FORCE_ABORT, val);
2005+
}
2006+
}
2007+
2008+
static void intel_tfa_commit_scheduling(struct cpu_hw_events *cpuc, int idx, int cntr)
2009+
{
2010+
/*
2011+
* We're going to use PMC3, make sure TFA is set before we touch it.
2012+
*/
2013+
if (cntr == 3 && !cpuc->is_fake)
2014+
intel_set_tfa(cpuc, true);
2015+
}
2016+
2017+
static void intel_tfa_pmu_enable_all(int added)
2018+
{
2019+
struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
2020+
2021+
/*
2022+
* If we find PMC3 is no longer used when we enable the PMU, we can
2023+
* clear TFA.
2024+
*/
2025+
if (!test_bit(3, cpuc->active_mask))
2026+
intel_set_tfa(cpuc, false);
2027+
2028+
intel_pmu_enable_all(added);
2029+
}
2030+
19982031
static inline u64 intel_pmu_get_status(void)
19992032
{
20002033
u64 status;
@@ -3238,6 +3271,26 @@ glp_get_event_constraints(struct cpu_hw_events *cpuc, int idx,
32383271
return c;
32393272
}
32403273

3274+
static bool allow_tsx_force_abort = true;
3275+
3276+
static struct event_constraint *
3277+
tfa_get_event_constraints(struct cpu_hw_events *cpuc, int idx,
3278+
struct perf_event *event)
3279+
{
3280+
struct event_constraint *c = hsw_get_event_constraints(cpuc, idx, event);
3281+
3282+
/*
3283+
* Without TFA we must not use PMC3.
3284+
*/
3285+
if (!allow_tsx_force_abort && test_bit(3, c->idxmsk)) {
3286+
c = dyn_constraint(cpuc, c, idx);
3287+
c->idxmsk64 &= ~(1ULL << 3);
3288+
c->weight--;
3289+
}
3290+
3291+
return c;
3292+
}
3293+
32413294
/*
32423295
* Broadwell:
32433296
*
@@ -3332,13 +3385,15 @@ int intel_cpuc_prepare(struct cpu_hw_events *cpuc, int cpu)
33323385
goto err;
33333386
}
33343387

3335-
if (x86_pmu.flags & PMU_FL_EXCL_CNTRS) {
3388+
if (x86_pmu.flags & (PMU_FL_EXCL_CNTRS | PMU_FL_TFA)) {
33363389
size_t sz = X86_PMC_IDX_MAX * sizeof(struct event_constraint);
33373390

33383391
cpuc->constraint_list = kzalloc_node(sz, GFP_KERNEL, cpu_to_node(cpu));
33393392
if (!cpuc->constraint_list)
33403393
goto err_shared_regs;
3394+
}
33413395

3396+
if (x86_pmu.flags & PMU_FL_EXCL_CNTRS) {
33423397
cpuc->excl_cntrs = allocate_excl_cntrs(cpu);
33433398
if (!cpuc->excl_cntrs)
33443399
goto err_constraint_list;
@@ -3445,9 +3500,10 @@ static void free_excl_cntrs(struct cpu_hw_events *cpuc)
34453500
if (c->core_id == -1 || --c->refcnt == 0)
34463501
kfree(c);
34473502
cpuc->excl_cntrs = NULL;
3448-
kfree(cpuc->constraint_list);
3449-
cpuc->constraint_list = NULL;
34503503
}
3504+
3505+
kfree(cpuc->constraint_list);
3506+
cpuc->constraint_list = NULL;
34513507
}
34523508

34533509
static void intel_pmu_cpu_dying(int cpu)
@@ -3933,8 +3989,11 @@ static struct attribute *intel_pmu_caps_attrs[] = {
39333989
NULL
39343990
};
39353991

3992+
DEVICE_BOOL_ATTR(allow_tsx_force_abort, 0644, allow_tsx_force_abort);
3993+
39363994
static struct attribute *intel_pmu_attrs[] = {
39373995
&dev_attr_freeze_on_smi.attr,
3996+
NULL, /* &dev_attr_allow_tsx_force_abort.attr.attr */
39383997
NULL,
39393998
};
39403999

@@ -4390,6 +4449,15 @@ __init int intel_pmu_init(void)
43904449
x86_pmu.cpu_events = get_hsw_events_attrs();
43914450
intel_pmu_pebs_data_source_skl(
43924451
boot_cpu_data.x86_model == INTEL_FAM6_SKYLAKE_X);
4452+
4453+
if (boot_cpu_has(X86_FEATURE_TSX_FORCE_ABORT)) {
4454+
x86_pmu.flags |= PMU_FL_TFA;
4455+
x86_pmu.get_event_constraints = tfa_get_event_constraints;
4456+
x86_pmu.enable_all = intel_tfa_pmu_enable_all;
4457+
x86_pmu.commit_scheduling = intel_tfa_commit_scheduling;
4458+
intel_pmu_attrs[1] = &dev_attr_allow_tsx_force_abort.attr.attr;
4459+
}
4460+
43934461
pr_cont("Skylake events, ");
43944462
name = "skylake";
43954463
break;

‎arch/x86/events/perf_event.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,11 @@ struct cpu_hw_events {
242242
struct intel_excl_cntrs *excl_cntrs;
243243
int excl_thread_id; /* 0 or 1 */
244244

245+
/*
246+
* SKL TSX_FORCE_ABORT shadow
247+
*/
248+
u64 tfa_shadow;
249+
245250
/*
246251
* AMD specific bits
247252
*/
@@ -679,6 +684,7 @@ do { \
679684
#define PMU_FL_EXCL_CNTRS 0x4 /* has exclusive counter requirements */
680685
#define PMU_FL_EXCL_ENABLED 0x8 /* exclusive counter active */
681686
#define PMU_FL_PEBS_ALL 0x10 /* all events are valid PEBS events */
687+
#define PMU_FL_TFA 0x20 /* deal with TSX force abort */
682688

683689
#define EVENT_VAR(_id) event_attr_##_id
684690
#define EVENT_PTR(_id) &event_attr_##_id.attr.attr

0 commit comments

Comments
 (0)