Skip to content

Commit ffb871b

Browse files
Gleb NatapovIngo Molnar
authored andcommitted
x86, perf: Disable non available architectural events
Intel CPUs report non-available architectural events in cpuid leaf 0AH.EBX. Use it to disable events that are not available according to CPU. Signed-off-by: Gleb Natapov <gleb@redhat.com> Signed-off-by: Peter Zijlstra <a.p.zijlstra@chello.nl> Link: http://lkml.kernel.org/r/1320929850-10480-7-git-send-email-gleb@redhat.com Signed-off-by: Ingo Molnar <mingo@elte.hu>
1 parent ac99b86 commit ffb871b

3 files changed

Lines changed: 42 additions & 5 deletions

File tree

‎arch/x86/include/asm/perf_event.h‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
(1 << (ARCH_PERFMON_UNHALTED_CORE_CYCLES_INDEX))
5858

5959
#define ARCH_PERFMON_BRANCH_MISSES_RETIRED 6
60+
#define ARCH_PERFMON_EVENTS_COUNT 7
6061

6162
/*
6263
* Intel "Architectural Performance Monitoring" CPUID
@@ -72,6 +73,19 @@ union cpuid10_eax {
7273
unsigned int full;
7374
};
7475

76+
union cpuid10_ebx {
77+
struct {
78+
unsigned int no_unhalted_core_cycles:1;
79+
unsigned int no_instructions_retired:1;
80+
unsigned int no_unhalted_reference_cycles:1;
81+
unsigned int no_llc_reference:1;
82+
unsigned int no_llc_misses:1;
83+
unsigned int no_branch_instruction_retired:1;
84+
unsigned int no_branch_misses_retired:1;
85+
} split;
86+
unsigned int full;
87+
};
88+
7589
union cpuid10_edx {
7690
struct {
7791
unsigned int num_counters_fixed:5;

‎arch/x86/kernel/cpu/perf_event.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,11 @@ struct x86_pmu {
285285
int num_counters_fixed;
286286
int cntval_bits;
287287
u64 cntval_mask;
288+
union {
289+
unsigned long events_maskl;
290+
unsigned long events_mask[BITS_TO_LONGS(ARCH_PERFMON_EVENTS_COUNT)];
291+
};
292+
int events_mask_len;
288293
int apic;
289294
u64 max_period;
290295
struct event_constraint *

‎arch/x86/kernel/cpu/perf_event_intel.c‎

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1552,13 +1552,23 @@ static void intel_sandybridge_quirks(void)
15521552
x86_pmu.pebs_constraints = NULL;
15531553
}
15541554

1555+
static const int intel_event_id_to_hw_id[] __initconst = {
1556+
PERF_COUNT_HW_CPU_CYCLES,
1557+
PERF_COUNT_HW_INSTRUCTIONS,
1558+
PERF_COUNT_HW_BUS_CYCLES,
1559+
PERF_COUNT_HW_CACHE_REFERENCES,
1560+
PERF_COUNT_HW_CACHE_MISSES,
1561+
PERF_COUNT_HW_BRANCH_INSTRUCTIONS,
1562+
PERF_COUNT_HW_BRANCH_MISSES,
1563+
};
1564+
15551565
__init int intel_pmu_init(void)
15561566
{
15571567
union cpuid10_edx edx;
15581568
union cpuid10_eax eax;
1569+
union cpuid10_ebx ebx;
15591570
unsigned int unused;
1560-
unsigned int ebx;
1561-
int version;
1571+
int version, bit;
15621572

15631573
if (!cpu_has(&boot_cpu_data, X86_FEATURE_ARCH_PERFMON)) {
15641574
switch (boot_cpu_data.x86) {
@@ -1574,8 +1584,8 @@ __init int intel_pmu_init(void)
15741584
* Check whether the Architectural PerfMon supports
15751585
* Branch Misses Retired hw_event or not.
15761586
*/
1577-
cpuid(10, &eax.full, &ebx, &unused, &edx.full);
1578-
if (eax.split.mask_length <= ARCH_PERFMON_BRANCH_MISSES_RETIRED)
1587+
cpuid(10, &eax.full, &ebx.full, &unused, &edx.full);
1588+
if (eax.split.mask_length < ARCH_PERFMON_EVENTS_COUNT)
15791589
return -ENODEV;
15801590

15811591
version = eax.split.version_id;
@@ -1651,14 +1661,15 @@ __init int intel_pmu_init(void)
16511661
/* UOPS_EXECUTED.CORE_ACTIVE_CYCLES,c=1,i=1 */
16521662
intel_perfmon_event_map[PERF_COUNT_HW_STALLED_CYCLES_BACKEND] = 0x1803fb1;
16531663

1654-
if (ebx & 0x40) {
1664+
if (ebx.split.no_branch_misses_retired) {
16551665
/*
16561666
* Erratum AAJ80 detected, we work it around by using
16571667
* the BR_MISP_EXEC.ANY event. This will over-count
16581668
* branch-misses, but it's still much better than the
16591669
* architectural event which is often completely bogus:
16601670
*/
16611671
intel_perfmon_event_map[PERF_COUNT_HW_BRANCH_MISSES] = 0x7f89;
1672+
ebx.split.no_branch_misses_retired = 0;
16621673

16631674
pr_cont("erratum AAJ80 worked around, ");
16641675
}
@@ -1738,5 +1749,12 @@ __init int intel_pmu_init(void)
17381749
break;
17391750
}
17401751
}
1752+
x86_pmu.events_maskl = ebx.full;
1753+
x86_pmu.events_mask_len = eax.split.mask_length;
1754+
1755+
/* disable event that reported as not presend by cpuid */
1756+
for_each_set_bit(bit, x86_pmu.events_mask, ARRAY_SIZE(intel_event_id_to_hw_id))
1757+
intel_perfmon_event_map[intel_event_id_to_hw_id[bit]] = 0;
1758+
17411759
return 0;
17421760
}

0 commit comments

Comments
 (0)