Commit | Line | Data |
---|---|---|
241771ef | 1 | /* |
cdd6c482 | 2 | * Performance events x86 architecture code |
241771ef | 3 | * |
98144511 IM |
4 | * Copyright (C) 2008 Thomas Gleixner <tglx@linutronix.de> |
5 | * Copyright (C) 2008-2009 Red Hat, Inc., Ingo Molnar | |
6 | * Copyright (C) 2009 Jaswinder Singh Rajput | |
7 | * Copyright (C) 2009 Advanced Micro Devices, Inc., Robert Richter | |
90eec103 | 8 | * Copyright (C) 2008-2009 Red Hat, Inc., Peter Zijlstra |
30dd568c | 9 | * Copyright (C) 2009 Intel Corporation, <markus.t.metzger@intel.com> |
1da53e02 | 10 | * Copyright (C) 2009 Google, Inc., Stephane Eranian |
241771ef IM |
11 | * |
12 | * For licencing details see kernel-base/COPYING | |
13 | */ | |
14 | ||
cdd6c482 | 15 | #include <linux/perf_event.h> |
241771ef IM |
16 | #include <linux/capability.h> |
17 | #include <linux/notifier.h> | |
18 | #include <linux/hardirq.h> | |
19 | #include <linux/kprobes.h> | |
eb008eb6 PG |
20 | #include <linux/export.h> |
21 | #include <linux/init.h> | |
241771ef | 22 | #include <linux/kdebug.h> |
589ee628 | 23 | #include <linux/sched/mm.h> |
e6017571 | 24 | #include <linux/sched/clock.h> |
d7d59fb3 | 25 | #include <linux/uaccess.h> |
5a0e3ad6 | 26 | #include <linux/slab.h> |
30dd568c | 27 | #include <linux/cpu.h> |
272d30be | 28 | #include <linux/bitops.h> |
0c9d42ed | 29 | #include <linux/device.h> |
46b1b577 | 30 | #include <linux/nospec.h> |
7c9903c9 | 31 | #include <linux/static_call.h> |
241771ef | 32 | |
241771ef | 33 | #include <asm/apic.h> |
d7d59fb3 | 34 | #include <asm/stacktrace.h> |
efef7f18 | 35 | #include <asm/msr.h> |
4e935e47 | 36 | #include <asm/nmi.h> |
69092624 | 37 | #include <asm/smp.h> |
c8e5910e | 38 | #include <asm/alternative.h> |
7911d3f7 | 39 | #include <asm/mmu_context.h> |
375074cc | 40 | #include <asm/tlbflush.h> |
e3f3541c | 41 | #include <asm/timer.h> |
d07bdfd3 PZ |
42 | #include <asm/desc.h> |
43 | #include <asm/ldt.h> | |
35f4d9b3 | 44 | #include <asm/unwind.h> |
cfa7f3d2 AN |
45 | #include <asm/uprobes.h> |
46 | #include <asm/ibt.h> | |
241771ef | 47 | |
27f6d22b | 48 | #include "perf_event.h" |
de0428a7 | 49 | |
de0428a7 | 50 | struct x86_pmu x86_pmu __read_mostly; |
61e76d53 | 51 | static struct pmu pmu; |
efc9f05d | 52 | |
de0428a7 | 53 | DEFINE_PER_CPU(struct cpu_hw_events, cpu_hw_events) = { |
b0f3f28e | 54 | .enabled = 1, |
61e76d53 | 55 | .pmu = &pmu, |
b0f3f28e | 56 | }; |
241771ef | 57 | |
405b4537 | 58 | DEFINE_STATIC_KEY_FALSE(rdpmc_never_available_key); |
631fe154 | 59 | DEFINE_STATIC_KEY_FALSE(rdpmc_always_available_key); |
d0946a88 | 60 | DEFINE_STATIC_KEY_FALSE(perf_is_hybrid); |
a6673429 | 61 | |
7c9903c9 PZ |
62 | /* |
63 | * This here uses DEFINE_STATIC_CALL_NULL() to get a static_call defined | |
64 | * from just a typename, as opposed to an actual function. | |
65 | */ | |
66 | DEFINE_STATIC_CALL_NULL(x86_pmu_handle_irq, *x86_pmu.handle_irq); | |
67 | DEFINE_STATIC_CALL_NULL(x86_pmu_disable_all, *x86_pmu.disable_all); | |
68 | DEFINE_STATIC_CALL_NULL(x86_pmu_enable_all, *x86_pmu.enable_all); | |
69 | DEFINE_STATIC_CALL_NULL(x86_pmu_enable, *x86_pmu.enable); | |
70 | DEFINE_STATIC_CALL_NULL(x86_pmu_disable, *x86_pmu.disable); | |
71 | ||
8b8ff8cc AH |
72 | DEFINE_STATIC_CALL_NULL(x86_pmu_assign, *x86_pmu.assign); |
73 | ||
7c9903c9 PZ |
74 | DEFINE_STATIC_CALL_NULL(x86_pmu_add, *x86_pmu.add); |
75 | DEFINE_STATIC_CALL_NULL(x86_pmu_del, *x86_pmu.del); | |
76 | DEFINE_STATIC_CALL_NULL(x86_pmu_read, *x86_pmu.read); | |
77 | ||
08b3068f PZ |
78 | DEFINE_STATIC_CALL_NULL(x86_pmu_set_period, *x86_pmu.set_period); |
79 | DEFINE_STATIC_CALL_NULL(x86_pmu_update, *x86_pmu.update); | |
80 | DEFINE_STATIC_CALL_NULL(x86_pmu_limit_period, *x86_pmu.limit_period); | |
73759c34 | 81 | |
7c9903c9 PZ |
82 | DEFINE_STATIC_CALL_NULL(x86_pmu_schedule_events, *x86_pmu.schedule_events); |
83 | DEFINE_STATIC_CALL_NULL(x86_pmu_get_event_constraints, *x86_pmu.get_event_constraints); | |
84 | DEFINE_STATIC_CALL_NULL(x86_pmu_put_event_constraints, *x86_pmu.put_event_constraints); | |
85 | ||
86 | DEFINE_STATIC_CALL_NULL(x86_pmu_start_scheduling, *x86_pmu.start_scheduling); | |
87 | DEFINE_STATIC_CALL_NULL(x86_pmu_commit_scheduling, *x86_pmu.commit_scheduling); | |
88 | DEFINE_STATIC_CALL_NULL(x86_pmu_stop_scheduling, *x86_pmu.stop_scheduling); | |
89 | ||
90 | DEFINE_STATIC_CALL_NULL(x86_pmu_sched_task, *x86_pmu.sched_task); | |
7c9903c9 PZ |
91 | |
92 | DEFINE_STATIC_CALL_NULL(x86_pmu_drain_pebs, *x86_pmu.drain_pebs); | |
93 | DEFINE_STATIC_CALL_NULL(x86_pmu_pebs_aliases, *x86_pmu.pebs_aliases); | |
94 | ||
bd275681 PZ |
95 | DEFINE_STATIC_CALL_NULL(x86_pmu_filter, *x86_pmu.filter); |
96 | ||
e02e9b03 KL |
97 | DEFINE_STATIC_CALL_NULL(x86_pmu_late_setup, *x86_pmu.late_setup); |
98 | ||
4a3fd130 DM |
99 | DEFINE_STATIC_CALL_NULL(x86_pmu_pebs_enable, *x86_pmu.pebs_enable); |
100 | DEFINE_STATIC_CALL_NULL(x86_pmu_pebs_disable, *x86_pmu.pebs_disable); | |
101 | DEFINE_STATIC_CALL_NULL(x86_pmu_pebs_enable_all, *x86_pmu.pebs_enable_all); | |
102 | DEFINE_STATIC_CALL_NULL(x86_pmu_pebs_disable_all, *x86_pmu.pebs_disable_all); | |
103 | ||
c8e2fe13 SC |
104 | /* |
105 | * This one is magic, it will get called even when PMU init fails (because | |
106 | * there is no PMU), in which case it should simply return NULL. | |
107 | */ | |
108 | DEFINE_STATIC_CALL_RET0(x86_pmu_guest_get_msrs, *x86_pmu.guest_get_msrs); | |
abd562df | 109 | |
de0428a7 | 110 | u64 __read_mostly hw_cache_event_ids |
8326f44d IM |
111 | [PERF_COUNT_HW_CACHE_MAX] |
112 | [PERF_COUNT_HW_CACHE_OP_MAX] | |
113 | [PERF_COUNT_HW_CACHE_RESULT_MAX]; | |
de0428a7 | 114 | u64 __read_mostly hw_cache_extra_regs |
e994d7d2 AK |
115 | [PERF_COUNT_HW_CACHE_MAX] |
116 | [PERF_COUNT_HW_CACHE_OP_MAX] | |
117 | [PERF_COUNT_HW_CACHE_RESULT_MAX]; | |
8326f44d | 118 | |
ee06094f | 119 | /* |
cdd6c482 IM |
120 | * Propagate event elapsed time into the generic event. |
121 | * Can only be executed on the CPU where the event is active. | |
ee06094f IM |
122 | * Returns the delta events processed. |
123 | */ | |
de0428a7 | 124 | u64 x86_perf_event_update(struct perf_event *event) |
ee06094f | 125 | { |
cc2ad4ba | 126 | struct hw_perf_event *hwc = &event->hw; |
948b1bb8 | 127 | int shift = 64 - x86_pmu.cntval_bits; |
ec3232bd | 128 | u64 prev_raw_count, new_raw_count; |
7f612a7f | 129 | u64 delta; |
ee06094f | 130 | |
027440b5 | 131 | if (unlikely(!hwc->event_base)) |
30dd568c MM |
132 | return 0; |
133 | ||
ee06094f | 134 | /* |
cdd6c482 | 135 | * Careful: an NMI might modify the previous event value. |
ee06094f IM |
136 | * |
137 | * Our tactic to handle this is to first atomically read and | |
138 | * exchange a new raw count - then add that new-prev delta | |
cdd6c482 | 139 | * count to the generic event atomically: |
ee06094f | 140 | */ |
e7850595 | 141 | prev_raw_count = local64_read(&hwc->prev_count); |
4c1c9dea | 142 | do { |
795ada52 | 143 | new_raw_count = rdpmc(hwc->event_base_rdpmc); |
4c1c9dea UB |
144 | } while (!local64_try_cmpxchg(&hwc->prev_count, |
145 | &prev_raw_count, new_raw_count)); | |
ee06094f IM |
146 | |
147 | /* | |
148 | * Now we have the new raw value and have updated the prev | |
149 | * timestamp already. We can now calculate the elapsed delta | |
cdd6c482 | 150 | * (event-)time and add that to the generic event. |
ee06094f IM |
151 | * |
152 | * Careful, not all hw sign-extends above the physical width | |
ec3232bd | 153 | * of the count. |
ee06094f | 154 | */ |
ec3232bd PZ |
155 | delta = (new_raw_count << shift) - (prev_raw_count << shift); |
156 | delta >>= shift; | |
ee06094f | 157 | |
e7850595 PZ |
158 | local64_add(delta, &event->count); |
159 | local64_sub(delta, &hwc->period_left); | |
4b7bfd0d RR |
160 | |
161 | return new_raw_count; | |
ee06094f IM |
162 | } |
163 | ||
a7e3ed1e AK |
164 | /* |
165 | * Find and validate any extra registers to set up. | |
166 | */ | |
167 | static int x86_pmu_extra_regs(u64 config, struct perf_event *event) | |
168 | { | |
183af736 | 169 | struct extra_reg *extra_regs = hybrid(event->pmu, extra_regs); |
efc9f05d | 170 | struct hw_perf_event_extra *reg; |
a7e3ed1e AK |
171 | struct extra_reg *er; |
172 | ||
efc9f05d | 173 | reg = &event->hw.extra_reg; |
a7e3ed1e | 174 | |
183af736 | 175 | if (!extra_regs) |
a7e3ed1e AK |
176 | return 0; |
177 | ||
183af736 | 178 | for (er = extra_regs; er->msr; er++) { |
a7e3ed1e AK |
179 | if (er->event != (config & er->config_mask)) |
180 | continue; | |
181 | if (event->attr.config1 & ~er->valid_mask) | |
182 | return -EINVAL; | |
338b522c KL |
183 | /* Check if the extra msrs can be safely accessed*/ |
184 | if (!er->extra_msr_access) | |
185 | return -ENXIO; | |
efc9f05d SE |
186 | |
187 | reg->idx = er->idx; | |
188 | reg->config = event->attr.config1; | |
189 | reg->reg = er->msr; | |
a7e3ed1e AK |
190 | break; |
191 | } | |
192 | return 0; | |
193 | } | |
194 | ||
cdd6c482 | 195 | static atomic_t active_events; |
1b7b938f | 196 | static atomic_t pmc_refcount; |
4e935e47 PZ |
197 | static DEFINE_MUTEX(pmc_reserve_mutex); |
198 | ||
b27ea29c RR |
199 | #ifdef CONFIG_X86_LOCAL_APIC |
200 | ||
722e42e4 | 201 | static inline u64 get_possible_counter_mask(void) |
d4b294bf | 202 | { |
722e42e4 KL |
203 | u64 cntr_mask = x86_pmu.cntr_mask64; |
204 | int i; | |
d4b294bf KL |
205 | |
206 | if (!is_hybrid()) | |
722e42e4 | 207 | return cntr_mask; |
d4b294bf KL |
208 | |
209 | for (i = 0; i < x86_pmu.num_hybrid_pmus; i++) | |
722e42e4 | 210 | cntr_mask |= x86_pmu.hybrid_pmu[i].cntr_mask64; |
d4b294bf | 211 | |
722e42e4 | 212 | return cntr_mask; |
d4b294bf KL |
213 | } |
214 | ||
4e935e47 PZ |
215 | static bool reserve_pmc_hardware(void) |
216 | { | |
722e42e4 KL |
217 | u64 cntr_mask = get_possible_counter_mask(); |
218 | int i, end; | |
4e935e47 | 219 | |
722e42e4 | 220 | for_each_set_bit(i, (unsigned long *)&cntr_mask, X86_PMC_IDX_MAX) { |
41bf4989 | 221 | if (!reserve_perfctr_nmi(x86_pmu_event_addr(i))) |
4e935e47 PZ |
222 | goto perfctr_fail; |
223 | } | |
224 | ||
722e42e4 | 225 | for_each_set_bit(i, (unsigned long *)&cntr_mask, X86_PMC_IDX_MAX) { |
41bf4989 | 226 | if (!reserve_evntsel_nmi(x86_pmu_config_addr(i))) |
4e935e47 PZ |
227 | goto eventsel_fail; |
228 | } | |
229 | ||
230 | return true; | |
231 | ||
232 | eventsel_fail: | |
722e42e4 KL |
233 | end = i; |
234 | for_each_set_bit(i, (unsigned long *)&cntr_mask, end) | |
41bf4989 | 235 | release_evntsel_nmi(x86_pmu_config_addr(i)); |
722e42e4 | 236 | i = X86_PMC_IDX_MAX; |
4e935e47 PZ |
237 | |
238 | perfctr_fail: | |
722e42e4 KL |
239 | end = i; |
240 | for_each_set_bit(i, (unsigned long *)&cntr_mask, end) | |
41bf4989 | 241 | release_perfctr_nmi(x86_pmu_event_addr(i)); |
4e935e47 | 242 | |
4e935e47 PZ |
243 | return false; |
244 | } | |
245 | ||
246 | static void release_pmc_hardware(void) | |
247 | { | |
722e42e4 KL |
248 | u64 cntr_mask = get_possible_counter_mask(); |
249 | int i; | |
4e935e47 | 250 | |
722e42e4 | 251 | for_each_set_bit(i, (unsigned long *)&cntr_mask, X86_PMC_IDX_MAX) { |
41bf4989 RR |
252 | release_perfctr_nmi(x86_pmu_event_addr(i)); |
253 | release_evntsel_nmi(x86_pmu_config_addr(i)); | |
4e935e47 | 254 | } |
4e935e47 PZ |
255 | } |
256 | ||
b27ea29c RR |
257 | #else |
258 | ||
259 | static bool reserve_pmc_hardware(void) { return true; } | |
260 | static void release_pmc_hardware(void) {} | |
261 | ||
262 | #endif | |
263 | ||
722e42e4 KL |
264 | bool check_hw_exists(struct pmu *pmu, unsigned long *cntr_mask, |
265 | unsigned long *fixed_cntr_mask) | |
33c6d6a7 | 266 | { |
11d8b058 AB |
267 | u64 val, val_fail = -1, val_new= ~0; |
268 | int i, reg, reg_fail = -1, ret = 0; | |
a5ebe0ba | 269 | int bios_fail = 0; |
68ab7476 | 270 | int reg_safe = -1; |
33c6d6a7 | 271 | |
4407204c PZ |
272 | /* |
273 | * Check to see if the BIOS enabled any of the counters, if so | |
274 | * complain and bail. | |
275 | */ | |
722e42e4 | 276 | for_each_set_bit(i, cntr_mask, X86_PMC_IDX_MAX) { |
41bf4989 | 277 | reg = x86_pmu_config_addr(i); |
6fe22aba | 278 | ret = rdmsrq_safe(reg, &val); |
4407204c PZ |
279 | if (ret) |
280 | goto msr_fail; | |
a5ebe0ba GD |
281 | if (val & ARCH_PERFMON_EVENTSEL_ENABLE) { |
282 | bios_fail = 1; | |
283 | val_fail = val; | |
284 | reg_fail = reg; | |
68ab7476 DZ |
285 | } else { |
286 | reg_safe = i; | |
a5ebe0ba | 287 | } |
4407204c PZ |
288 | } |
289 | ||
722e42e4 | 290 | if (*(u64 *)fixed_cntr_mask) { |
4407204c | 291 | reg = MSR_ARCH_PERFMON_FIXED_CTR_CTRL; |
6fe22aba | 292 | ret = rdmsrq_safe(reg, &val); |
4407204c PZ |
293 | if (ret) |
294 | goto msr_fail; | |
722e42e4 | 295 | for_each_set_bit(i, fixed_cntr_mask, X86_PMC_IDX_MAX) { |
fc4b8fca | 296 | if (fixed_counter_disabled(i, pmu)) |
32451614 | 297 | continue; |
32d35c4a | 298 | if (val & (0x03ULL << i*4)) { |
a5ebe0ba GD |
299 | bios_fail = 1; |
300 | val_fail = val; | |
301 | reg_fail = reg; | |
302 | } | |
4407204c PZ |
303 | } |
304 | } | |
305 | ||
68ab7476 DZ |
306 | /* |
307 | * If all the counters are enabled, the below test will always | |
308 | * fail. The tools will also become useless in this scenario. | |
309 | * Just fail and disable the hardware counters. | |
310 | */ | |
311 | ||
312 | if (reg_safe == -1) { | |
313 | reg = reg_safe; | |
314 | goto msr_fail; | |
315 | } | |
316 | ||
4407204c | 317 | /* |
bffd5fc2 AP |
318 | * Read the current value, change it and read it back to see if it |
319 | * matches, this is needed to detect certain hardware emulators | |
320 | * (qemu/kvm) that don't trap on the MSR access and always return 0s. | |
4407204c | 321 | */ |
68ab7476 | 322 | reg = x86_pmu_event_addr(reg_safe); |
6fe22aba | 323 | if (rdmsrq_safe(reg, &val)) |
bffd5fc2 AP |
324 | goto msr_fail; |
325 | val ^= 0xffffUL; | |
6fa17efe | 326 | ret = wrmsrq_safe(reg, val); |
6fe22aba | 327 | ret |= rdmsrq_safe(reg, &val_new); |
33c6d6a7 | 328 | if (ret || val != val_new) |
4407204c | 329 | goto msr_fail; |
33c6d6a7 | 330 | |
45daae57 IM |
331 | /* |
332 | * We still allow the PMU driver to operate: | |
333 | */ | |
a5ebe0ba | 334 | if (bios_fail) { |
1b74dde7 CY |
335 | pr_cont("Broken BIOS detected, complain to your hardware vendor.\n"); |
336 | pr_err(FW_BUG "the BIOS has corrupted hw-PMU resources (MSR %x is %Lx)\n", | |
337 | reg_fail, val_fail); | |
a5ebe0ba | 338 | } |
45daae57 IM |
339 | |
340 | return true; | |
4407204c PZ |
341 | |
342 | msr_fail: | |
005bd007 JG |
343 | if (boot_cpu_has(X86_FEATURE_HYPERVISOR)) { |
344 | pr_cont("PMU not available due to virtualization, using software events only.\n"); | |
345 | } else { | |
346 | pr_cont("Broken PMU hardware detected, using software events only.\n"); | |
347 | pr_err("Failed to access perfctr msr (MSR %x is %Lx)\n", | |
348 | reg, val_new); | |
349 | } | |
45daae57 | 350 | |
4407204c | 351 | return false; |
33c6d6a7 DZ |
352 | } |
353 | ||
cdd6c482 | 354 | static void hw_perf_event_destroy(struct perf_event *event) |
4e935e47 | 355 | { |
6b099d9b | 356 | x86_release_hardware(); |
1b7b938f | 357 | atomic_dec(&active_events); |
4e935e47 PZ |
358 | } |
359 | ||
48070342 AS |
360 | void hw_perf_lbr_event_destroy(struct perf_event *event) |
361 | { | |
362 | hw_perf_event_destroy(event); | |
363 | ||
364 | /* undo the lbr/bts event accounting */ | |
365 | x86_del_exclusive(x86_lbr_exclusive_lbr); | |
366 | } | |
367 | ||
85cf9dba RR |
368 | static inline int x86_pmu_initialized(void) |
369 | { | |
370 | return x86_pmu.handle_irq != NULL; | |
371 | } | |
372 | ||
8326f44d | 373 | static inline int |
e994d7d2 | 374 | set_ext_hw_attr(struct hw_perf_event *hwc, struct perf_event *event) |
8326f44d | 375 | { |
e994d7d2 | 376 | struct perf_event_attr *attr = &event->attr; |
8326f44d IM |
377 | unsigned int cache_type, cache_op, cache_result; |
378 | u64 config, val; | |
379 | ||
380 | config = attr->config; | |
381 | ||
ef9ee4ad | 382 | cache_type = (config >> 0) & 0xff; |
8326f44d IM |
383 | if (cache_type >= PERF_COUNT_HW_CACHE_MAX) |
384 | return -EINVAL; | |
ef9ee4ad | 385 | cache_type = array_index_nospec(cache_type, PERF_COUNT_HW_CACHE_MAX); |
8326f44d IM |
386 | |
387 | cache_op = (config >> 8) & 0xff; | |
388 | if (cache_op >= PERF_COUNT_HW_CACHE_OP_MAX) | |
389 | return -EINVAL; | |
ef9ee4ad | 390 | cache_op = array_index_nospec(cache_op, PERF_COUNT_HW_CACHE_OP_MAX); |
8326f44d IM |
391 | |
392 | cache_result = (config >> 16) & 0xff; | |
393 | if (cache_result >= PERF_COUNT_HW_CACHE_RESULT_MAX) | |
394 | return -EINVAL; | |
ef9ee4ad | 395 | cache_result = array_index_nospec(cache_result, PERF_COUNT_HW_CACHE_RESULT_MAX); |
8326f44d | 396 | |
0d18f2df | 397 | val = hybrid_var(event->pmu, hw_cache_event_ids)[cache_type][cache_op][cache_result]; |
8326f44d IM |
398 | if (val == 0) |
399 | return -ENOENT; | |
400 | ||
401 | if (val == -1) | |
402 | return -EINVAL; | |
403 | ||
404 | hwc->config |= val; | |
0d18f2df | 405 | attr->config1 = hybrid_var(event->pmu, hw_cache_extra_regs)[cache_type][cache_op][cache_result]; |
e994d7d2 | 406 | return x86_pmu_extra_regs(val, event); |
8326f44d IM |
407 | } |
408 | ||
6b099d9b AS |
409 | int x86_reserve_hardware(void) |
410 | { | |
411 | int err = 0; | |
412 | ||
1b7b938f | 413 | if (!atomic_inc_not_zero(&pmc_refcount)) { |
6b099d9b | 414 | mutex_lock(&pmc_reserve_mutex); |
1b7b938f | 415 | if (atomic_read(&pmc_refcount) == 0) { |
488e13a4 | 416 | if (!reserve_pmc_hardware()) { |
6b099d9b | 417 | err = -EBUSY; |
488e13a4 | 418 | } else { |
6b099d9b | 419 | reserve_ds_buffers(); |
488e13a4 LX |
420 | reserve_lbr_buffers(); |
421 | } | |
6b099d9b AS |
422 | } |
423 | if (!err) | |
1b7b938f | 424 | atomic_inc(&pmc_refcount); |
6b099d9b AS |
425 | mutex_unlock(&pmc_reserve_mutex); |
426 | } | |
427 | ||
428 | return err; | |
429 | } | |
430 | ||
431 | void x86_release_hardware(void) | |
432 | { | |
1b7b938f | 433 | if (atomic_dec_and_mutex_lock(&pmc_refcount, &pmc_reserve_mutex)) { |
6b099d9b AS |
434 | release_pmc_hardware(); |
435 | release_ds_buffers(); | |
c085fb87 | 436 | release_lbr_buffers(); |
6b099d9b AS |
437 | mutex_unlock(&pmc_reserve_mutex); |
438 | } | |
439 | } | |
440 | ||
48070342 AS |
441 | /* |
442 | * Check if we can create event of a certain type (that no conflicting events | |
443 | * are present). | |
444 | */ | |
445 | int x86_add_exclusive(unsigned int what) | |
446 | { | |
93472aff | 447 | int i; |
48070342 | 448 | |
b0c1ef52 AK |
449 | /* |
450 | * When lbr_pt_coexist we allow PT to coexist with either LBR or BTS. | |
451 | * LBR and BTS are still mutually exclusive. | |
452 | */ | |
453 | if (x86_pmu.lbr_pt_coexist && what == x86_lbr_exclusive_pt) | |
92ca7da4 | 454 | goto out; |
ccbebba4 | 455 | |
93472aff PZ |
456 | if (!atomic_inc_not_zero(&x86_pmu.lbr_exclusive[what])) { |
457 | mutex_lock(&pmc_reserve_mutex); | |
458 | for (i = 0; i < ARRAY_SIZE(x86_pmu.lbr_exclusive); i++) { | |
459 | if (i != what && atomic_read(&x86_pmu.lbr_exclusive[i])) | |
460 | goto fail_unlock; | |
461 | } | |
462 | atomic_inc(&x86_pmu.lbr_exclusive[what]); | |
463 | mutex_unlock(&pmc_reserve_mutex); | |
6b099d9b | 464 | } |
48070342 | 465 | |
92ca7da4 | 466 | out: |
93472aff PZ |
467 | atomic_inc(&active_events); |
468 | return 0; | |
48070342 | 469 | |
93472aff | 470 | fail_unlock: |
48070342 | 471 | mutex_unlock(&pmc_reserve_mutex); |
93472aff | 472 | return -EBUSY; |
48070342 AS |
473 | } |
474 | ||
475 | void x86_del_exclusive(unsigned int what) | |
476 | { | |
92ca7da4 AS |
477 | atomic_dec(&active_events); |
478 | ||
479 | /* | |
480 | * See the comment in x86_add_exclusive(). | |
481 | */ | |
b0c1ef52 | 482 | if (x86_pmu.lbr_pt_coexist && what == x86_lbr_exclusive_pt) |
ccbebba4 AS |
483 | return; |
484 | ||
48070342 AS |
485 | atomic_dec(&x86_pmu.lbr_exclusive[what]); |
486 | } | |
487 | ||
de0428a7 | 488 | int x86_setup_perfctr(struct perf_event *event) |
c1726f34 RR |
489 | { |
490 | struct perf_event_attr *attr = &event->attr; | |
491 | struct hw_perf_event *hwc = &event->hw; | |
492 | u64 config; | |
493 | ||
6c7e550f | 494 | if (!is_sampling_event(event)) { |
c1726f34 RR |
495 | hwc->sample_period = x86_pmu.max_period; |
496 | hwc->last_period = hwc->sample_period; | |
e7850595 | 497 | local64_set(&hwc->period_left, hwc->sample_period); |
c1726f34 RR |
498 | } |
499 | ||
d9977c43 | 500 | if (attr->type == event->pmu->type) |
ed13ec58 | 501 | return x86_pmu_extra_regs(event->attr.config, event); |
c1726f34 RR |
502 | |
503 | if (attr->type == PERF_TYPE_HW_CACHE) | |
e994d7d2 | 504 | return set_ext_hw_attr(hwc, event); |
c1726f34 RR |
505 | |
506 | if (attr->config >= x86_pmu.max_events) | |
507 | return -EINVAL; | |
508 | ||
46b1b577 PZ |
509 | attr->config = array_index_nospec((unsigned long)attr->config, x86_pmu.max_events); |
510 | ||
c1726f34 RR |
511 | /* |
512 | * The generic map: | |
513 | */ | |
514 | config = x86_pmu.event_map(attr->config); | |
515 | ||
516 | if (config == 0) | |
517 | return -ENOENT; | |
518 | ||
519 | if (config == -1LL) | |
520 | return -EINVAL; | |
521 | ||
c1726f34 RR |
522 | hwc->config |= config; |
523 | ||
524 | return 0; | |
525 | } | |
4261e0e0 | 526 | |
ff3fb511 SE |
527 | /* |
528 | * check that branch_sample_type is compatible with | |
529 | * settings needed for precise_ip > 1 which implies | |
530 | * using the LBR to capture ALL taken branches at the | |
531 | * priv levels of the measurement | |
532 | */ | |
533 | static inline int precise_br_compat(struct perf_event *event) | |
534 | { | |
535 | u64 m = event->attr.branch_sample_type; | |
536 | u64 b = 0; | |
537 | ||
538 | /* must capture all branches */ | |
539 | if (!(m & PERF_SAMPLE_BRANCH_ANY)) | |
540 | return 0; | |
541 | ||
542 | m &= PERF_SAMPLE_BRANCH_KERNEL | PERF_SAMPLE_BRANCH_USER; | |
543 | ||
544 | if (!event->attr.exclude_user) | |
545 | b |= PERF_SAMPLE_BRANCH_USER; | |
546 | ||
547 | if (!event->attr.exclude_kernel) | |
548 | b |= PERF_SAMPLE_BRANCH_KERNEL; | |
549 | ||
550 | /* | |
551 | * ignore PERF_SAMPLE_BRANCH_HV, not supported on x86 | |
552 | */ | |
553 | ||
554 | return m == b; | |
555 | } | |
556 | ||
b00233b5 | 557 | int x86_pmu_max_precise(void) |
a072738e | 558 | { |
b00233b5 AK |
559 | int precise = 0; |
560 | ||
561 | /* Support for constant skid */ | |
562 | if (x86_pmu.pebs_active && !x86_pmu.pebs_broken) { | |
563 | precise++; | |
ab608344 | 564 | |
b00233b5 AK |
565 | /* Support for IP fixup */ |
566 | if (x86_pmu.lbr_nr || x86_pmu.intel_cap.pebs_format >= 2) | |
ab608344 PZ |
567 | precise++; |
568 | ||
b00233b5 AK |
569 | if (x86_pmu.pebs_prec_dist) |
570 | precise++; | |
571 | } | |
572 | return precise; | |
573 | } | |
72469764 | 574 | |
b00233b5 AK |
575 | int x86_pmu_hw_config(struct perf_event *event) |
576 | { | |
577 | if (event->attr.precise_ip) { | |
578 | int precise = x86_pmu_max_precise(); | |
ab608344 PZ |
579 | |
580 | if (event->attr.precise_ip > precise) | |
581 | return -EOPNOTSUPP; | |
18e7a45a JO |
582 | |
583 | /* There's no sense in having PEBS for non sampling events: */ | |
584 | if (!is_sampling_event(event)) | |
585 | return -EINVAL; | |
4b854900 YZ |
586 | } |
587 | /* | |
588 | * check that PEBS LBR correction does not conflict with | |
589 | * whatever the user is asking with attr->branch_sample_type | |
590 | */ | |
591 | if (event->attr.precise_ip > 1 && x86_pmu.intel_cap.pebs_format < 2) { | |
592 | u64 *br_type = &event->attr.branch_sample_type; | |
593 | ||
594 | if (has_branch_stack(event)) { | |
595 | if (!precise_br_compat(event)) | |
596 | return -EOPNOTSUPP; | |
597 | ||
598 | /* branch_sample_type is compatible */ | |
599 | ||
600 | } else { | |
601 | /* | |
602 | * user did not specify branch_sample_type | |
603 | * | |
604 | * For PEBS fixups, we capture all | |
605 | * the branches at the priv level of the | |
606 | * event. | |
607 | */ | |
608 | *br_type = PERF_SAMPLE_BRANCH_ANY; | |
609 | ||
610 | if (!event->attr.exclude_user) | |
611 | *br_type |= PERF_SAMPLE_BRANCH_USER; | |
612 | ||
613 | if (!event->attr.exclude_kernel) | |
614 | *br_type |= PERF_SAMPLE_BRANCH_KERNEL; | |
ff3fb511 | 615 | } |
ab608344 PZ |
616 | } |
617 | ||
1f2376cd | 618 | if (branch_sample_call_stack(event)) |
e18bf526 YZ |
619 | event->attach_state |= PERF_ATTACH_TASK_DATA; |
620 | ||
a072738e CG |
621 | /* |
622 | * Generate PMC IRQs: | |
623 | * (keep 'enabled' bit clear for now) | |
624 | */ | |
b4cdc5c2 | 625 | event->hw.config = ARCH_PERFMON_EVENTSEL_INT; |
a072738e CG |
626 | |
627 | /* | |
628 | * Count user and OS events unless requested not to | |
629 | */ | |
b4cdc5c2 PZ |
630 | if (!event->attr.exclude_user) |
631 | event->hw.config |= ARCH_PERFMON_EVENTSEL_USR; | |
632 | if (!event->attr.exclude_kernel) | |
633 | event->hw.config |= ARCH_PERFMON_EVENTSEL_OS; | |
a072738e | 634 | |
d9977c43 | 635 | if (event->attr.type == event->pmu->type) |
e8fb5d6e | 636 | event->hw.config |= x86_pmu_get_event_config(event); |
a072738e | 637 | |
1a97fea9 | 638 | if (is_sampling_event(event) && !event->attr.freq && x86_pmu.limit_period) { |
28f0f3c4 PZ |
639 | s64 left = event->attr.sample_period; |
640 | x86_pmu.limit_period(event, &left); | |
641 | if (left > event->attr.sample_period) | |
294fe0f5 AK |
642 | return -EINVAL; |
643 | } | |
644 | ||
878068ea | 645 | /* sample_regs_user never support XMM registers */ |
dce86ac7 | 646 | if (unlikely(event->attr.sample_regs_user & PERF_REG_EXTENDED_MASK)) |
878068ea KL |
647 | return -EINVAL; |
648 | /* | |
649 | * Besides the general purpose registers, XMM registers may | |
650 | * be collected in PEBS on some platforms, e.g. Icelake | |
651 | */ | |
dce86ac7 | 652 | if (unlikely(event->attr.sample_regs_intr & PERF_REG_EXTENDED_MASK)) { |
cd6b984f | 653 | if (!(event->pmu->capabilities & PERF_PMU_CAP_EXTENDED_REGS)) |
878068ea KL |
654 | return -EINVAL; |
655 | ||
656 | if (!event->attr.precise_ip) | |
657 | return -EINVAL; | |
658 | } | |
659 | ||
9d0fcba6 | 660 | return x86_setup_perfctr(event); |
a098f448 RR |
661 | } |
662 | ||
241771ef | 663 | /* |
0d48696f | 664 | * Setup the hardware configuration for a given attr_type |
241771ef | 665 | */ |
b0a873eb | 666 | static int __x86_pmu_event_init(struct perf_event *event) |
241771ef | 667 | { |
4e935e47 | 668 | int err; |
241771ef | 669 | |
85cf9dba RR |
670 | if (!x86_pmu_initialized()) |
671 | return -ENODEV; | |
241771ef | 672 | |
6b099d9b | 673 | err = x86_reserve_hardware(); |
4e935e47 PZ |
674 | if (err) |
675 | return err; | |
676 | ||
1b7b938f | 677 | atomic_inc(&active_events); |
cdd6c482 | 678 | event->destroy = hw_perf_event_destroy; |
a1792cda | 679 | |
4261e0e0 RR |
680 | event->hw.idx = -1; |
681 | event->hw.last_cpu = -1; | |
682 | event->hw.last_tag = ~0ULL; | |
4dfe3232 | 683 | event->hw.dyn_constraint = ~0ULL; |
b690081d | 684 | |
efc9f05d SE |
685 | /* mark unused */ |
686 | event->hw.extra_reg.idx = EXTRA_REG_NONE; | |
b36817e8 SE |
687 | event->hw.branch_reg.idx = EXTRA_REG_NONE; |
688 | ||
9d0fcba6 | 689 | return x86_pmu.hw_config(event); |
4261e0e0 RR |
690 | } |
691 | ||
de0428a7 | 692 | void x86_pmu_disable_all(void) |
f87ad35d | 693 | { |
89cbc767 | 694 | struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); |
9e35ad38 PZ |
695 | int idx; |
696 | ||
722e42e4 | 697 | for_each_set_bit(idx, x86_pmu.cntr_mask, X86_PMC_IDX_MAX) { |
57388912 | 698 | struct hw_perf_event *hwc = &cpuc->events[idx]->hw; |
b0f3f28e PZ |
699 | u64 val; |
700 | ||
43f6201a | 701 | if (!test_bit(idx, cpuc->active_mask)) |
4295ee62 | 702 | continue; |
c435e608 | 703 | rdmsrq(x86_pmu_config_addr(idx), val); |
bb1165d6 | 704 | if (!(val & ARCH_PERFMON_EVENTSEL_ENABLE)) |
4295ee62 | 705 | continue; |
bb1165d6 | 706 | val &= ~ARCH_PERFMON_EVENTSEL_ENABLE; |
78255eb2 | 707 | wrmsrq(x86_pmu_config_addr(idx), val); |
57388912 | 708 | if (is_counter_pair(hwc)) |
78255eb2 | 709 | wrmsrq(x86_pmu_config_addr(idx + 1), 0); |
f87ad35d | 710 | } |
f87ad35d JSR |
711 | } |
712 | ||
39a4d779 | 713 | struct perf_guest_switch_msr *perf_guest_get_msrs(int *nr, void *data) |
abd562df | 714 | { |
39a4d779 | 715 | return static_call(x86_pmu_guest_get_msrs)(nr, data); |
abd562df LX |
716 | } |
717 | EXPORT_SYMBOL_GPL(perf_guest_get_msrs); | |
718 | ||
c3d266c8 KL |
719 | /* |
720 | * There may be PMI landing after enabled=0. The PMI hitting could be before or | |
721 | * after disable_all. | |
722 | * | |
723 | * If PMI hits before disable_all, the PMU will be disabled in the NMI handler. | |
724 | * It will not be re-enabled in the NMI handler again, because enabled=0. After | |
725 | * handling the NMI, disable_all will be called, which will not change the | |
726 | * state either. If PMI hits after disable_all, the PMU is already disabled | |
727 | * before entering NMI handler. The NMI handler will not change the state | |
728 | * either. | |
729 | * | |
730 | * So either situation is harmless. | |
731 | */ | |
a4eaf7f1 | 732 | static void x86_pmu_disable(struct pmu *pmu) |
b56a3802 | 733 | { |
89cbc767 | 734 | struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); |
1da53e02 | 735 | |
85cf9dba | 736 | if (!x86_pmu_initialized()) |
9e35ad38 | 737 | return; |
1da53e02 | 738 | |
1a6e21f7 PZ |
739 | if (!cpuc->enabled) |
740 | return; | |
741 | ||
742 | cpuc->n_added = 0; | |
743 | cpuc->enabled = 0; | |
744 | barrier(); | |
1da53e02 | 745 | |
7c9903c9 | 746 | static_call(x86_pmu_disable_all)(); |
b56a3802 | 747 | } |
241771ef | 748 | |
de0428a7 | 749 | void x86_pmu_enable_all(int added) |
f87ad35d | 750 | { |
89cbc767 | 751 | struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); |
f87ad35d JSR |
752 | int idx; |
753 | ||
722e42e4 | 754 | for_each_set_bit(idx, x86_pmu.cntr_mask, X86_PMC_IDX_MAX) { |
d45dd923 | 755 | struct hw_perf_event *hwc = &cpuc->events[idx]->hw; |
b0f3f28e | 756 | |
43f6201a | 757 | if (!test_bit(idx, cpuc->active_mask)) |
4295ee62 | 758 | continue; |
984b838c | 759 | |
d45dd923 | 760 | __x86_pmu_enable_event(hwc, ARCH_PERFMON_EVENTSEL_ENABLE); |
f87ad35d JSR |
761 | } |
762 | } | |
763 | ||
ec980e4f | 764 | int is_x86_event(struct perf_event *event) |
1da53e02 | 765 | { |
3e830f65 KL |
766 | /* |
767 | * For a non-hybrid platforms, the type of X86 pmu is | |
768 | * always PERF_TYPE_RAW. | |
769 | * For a hybrid platform, the PERF_PMU_CAP_EXTENDED_HW_TYPE | |
770 | * is a unique capability for the X86 PMU. | |
771 | * Use them to detect a X86 event. | |
772 | */ | |
773 | if (event->pmu->type == PERF_TYPE_RAW || | |
774 | event->pmu->capabilities & PERF_PMU_CAP_EXTENDED_HW_TYPE) | |
775 | return true; | |
d9977c43 KL |
776 | |
777 | return false; | |
1da53e02 SE |
778 | } |
779 | ||
61e76d53 | 780 | struct pmu *x86_get_pmu(unsigned int cpu) |
f447e4eb | 781 | { |
61e76d53 KL |
782 | struct cpu_hw_events *cpuc = &per_cpu(cpu_hw_events, cpu); |
783 | ||
784 | /* | |
785 | * All CPUs of the hybrid type have been offline. | |
786 | * The x86_get_pmu() should not be invoked. | |
787 | */ | |
788 | if (WARN_ON_ONCE(!cpuc->pmu)) | |
789 | return &pmu; | |
790 | ||
791 | return cpuc->pmu; | |
f447e4eb | 792 | } |
1e2ad28f RR |
793 | /* |
794 | * Event scheduler state: | |
795 | * | |
796 | * Assign events iterating over all events and counters, beginning | |
797 | * with events with least weights first. Keep the current iterator | |
798 | * state in struct sched_state. | |
799 | */ | |
800 | struct sched_state { | |
801 | int weight; | |
802 | int event; /* event index */ | |
803 | int counter; /* counter index */ | |
804 | int unassigned; /* number of events to be assigned left */ | |
cc1790cf | 805 | int nr_gp; /* number of GP counters used */ |
57388912 | 806 | u64 used; |
1e2ad28f RR |
807 | }; |
808 | ||
bc1738f6 RR |
809 | /* Total max is X86_PMC_IDX_MAX, but we are O(n!) limited */ |
810 | #define SCHED_STATES_MAX 2 | |
811 | ||
1e2ad28f RR |
812 | struct perf_sched { |
813 | int max_weight; | |
814 | int max_events; | |
cc1790cf PZ |
815 | int max_gp; |
816 | int saved_states; | |
b371b594 | 817 | struct event_constraint **constraints; |
1e2ad28f | 818 | struct sched_state state; |
bc1738f6 | 819 | struct sched_state saved[SCHED_STATES_MAX]; |
1e2ad28f RR |
820 | }; |
821 | ||
822 | /* | |
d9f6e12f | 823 | * Initialize iterator that runs through all events and counters. |
1e2ad28f | 824 | */ |
b371b594 | 825 | static void perf_sched_init(struct perf_sched *sched, struct event_constraint **constraints, |
cc1790cf | 826 | int num, int wmin, int wmax, int gpmax) |
1e2ad28f RR |
827 | { |
828 | int idx; | |
829 | ||
830 | memset(sched, 0, sizeof(*sched)); | |
831 | sched->max_events = num; | |
832 | sched->max_weight = wmax; | |
cc1790cf | 833 | sched->max_gp = gpmax; |
b371b594 | 834 | sched->constraints = constraints; |
1e2ad28f RR |
835 | |
836 | for (idx = 0; idx < num; idx++) { | |
b371b594 | 837 | if (constraints[idx]->weight == wmin) |
1e2ad28f RR |
838 | break; |
839 | } | |
840 | ||
841 | sched->state.event = idx; /* start with min weight */ | |
842 | sched->state.weight = wmin; | |
843 | sched->state.unassigned = num; | |
844 | } | |
845 | ||
bc1738f6 RR |
846 | static void perf_sched_save_state(struct perf_sched *sched) |
847 | { | |
848 | if (WARN_ON_ONCE(sched->saved_states >= SCHED_STATES_MAX)) | |
849 | return; | |
850 | ||
851 | sched->saved[sched->saved_states] = sched->state; | |
852 | sched->saved_states++; | |
853 | } | |
854 | ||
855 | static bool perf_sched_restore_state(struct perf_sched *sched) | |
856 | { | |
857 | if (!sched->saved_states) | |
858 | return false; | |
859 | ||
860 | sched->saved_states--; | |
861 | sched->state = sched->saved[sched->saved_states]; | |
862 | ||
57388912 KP |
863 | /* this assignment didn't work out */ |
864 | /* XXX broken vs EVENT_PAIR */ | |
865 | sched->state.used &= ~BIT_ULL(sched->state.counter); | |
866 | ||
867 | /* try the next one */ | |
868 | sched->state.counter++; | |
bc1738f6 RR |
869 | |
870 | return true; | |
871 | } | |
872 | ||
1e2ad28f RR |
873 | /* |
874 | * Select a counter for the current event to schedule. Return true on | |
875 | * success. | |
876 | */ | |
bc1738f6 | 877 | static bool __perf_sched_find_counter(struct perf_sched *sched) |
1e2ad28f RR |
878 | { |
879 | struct event_constraint *c; | |
880 | int idx; | |
881 | ||
882 | if (!sched->state.unassigned) | |
883 | return false; | |
884 | ||
885 | if (sched->state.event >= sched->max_events) | |
886 | return false; | |
887 | ||
b371b594 | 888 | c = sched->constraints[sched->state.event]; |
4defea85 | 889 | /* Prefer fixed purpose counters */ |
15c7ad51 RR |
890 | if (c->idxmsk64 & (~0ULL << INTEL_PMC_IDX_FIXED)) { |
891 | idx = INTEL_PMC_IDX_FIXED; | |
307b1cd7 | 892 | for_each_set_bit_from(idx, c->idxmsk, X86_PMC_IDX_MAX) { |
57388912 KP |
893 | u64 mask = BIT_ULL(idx); |
894 | ||
895 | if (sched->state.used & mask) | |
896 | continue; | |
897 | ||
898 | sched->state.used |= mask; | |
899 | goto done; | |
4defea85 PZ |
900 | } |
901 | } | |
cc1790cf | 902 | |
1e2ad28f RR |
903 | /* Grab the first unused counter starting with idx */ |
904 | idx = sched->state.counter; | |
15c7ad51 | 905 | for_each_set_bit_from(idx, c->idxmsk, INTEL_PMC_IDX_FIXED) { |
57388912 | 906 | u64 mask = BIT_ULL(idx); |
cc1790cf | 907 | |
57388912 KP |
908 | if (c->flags & PERF_X86_EVENT_PAIR) |
909 | mask |= mask << 1; | |
910 | ||
911 | if (sched->state.used & mask) | |
912 | continue; | |
913 | ||
914 | if (sched->state.nr_gp++ >= sched->max_gp) | |
915 | return false; | |
916 | ||
917 | sched->state.used |= mask; | |
918 | goto done; | |
1e2ad28f | 919 | } |
1e2ad28f | 920 | |
4defea85 PZ |
921 | return false; |
922 | ||
923 | done: | |
924 | sched->state.counter = idx; | |
1e2ad28f | 925 | |
bc1738f6 RR |
926 | if (c->overlap) |
927 | perf_sched_save_state(sched); | |
928 | ||
929 | return true; | |
930 | } | |
931 | ||
932 | static bool perf_sched_find_counter(struct perf_sched *sched) | |
933 | { | |
934 | while (!__perf_sched_find_counter(sched)) { | |
935 | if (!perf_sched_restore_state(sched)) | |
936 | return false; | |
937 | } | |
938 | ||
1e2ad28f RR |
939 | return true; |
940 | } | |
941 | ||
942 | /* | |
943 | * Go through all unassigned events and find the next one to schedule. | |
944 | * Take events with the least weight first. Return true on success. | |
945 | */ | |
946 | static bool perf_sched_next_event(struct perf_sched *sched) | |
947 | { | |
948 | struct event_constraint *c; | |
949 | ||
950 | if (!sched->state.unassigned || !--sched->state.unassigned) | |
951 | return false; | |
952 | ||
953 | do { | |
954 | /* next event */ | |
955 | sched->state.event++; | |
956 | if (sched->state.event >= sched->max_events) { | |
957 | /* next weight */ | |
958 | sched->state.event = 0; | |
959 | sched->state.weight++; | |
960 | if (sched->state.weight > sched->max_weight) | |
961 | return false; | |
962 | } | |
b371b594 | 963 | c = sched->constraints[sched->state.event]; |
1e2ad28f RR |
964 | } while (c->weight != sched->state.weight); |
965 | ||
966 | sched->state.counter = 0; /* start with first counter */ | |
967 | ||
968 | return true; | |
969 | } | |
970 | ||
971 | /* | |
972 | * Assign a counter for each event. | |
973 | */ | |
b371b594 | 974 | int perf_assign_events(struct event_constraint **constraints, int n, |
cc1790cf | 975 | int wmin, int wmax, int gpmax, int *assign) |
1e2ad28f RR |
976 | { |
977 | struct perf_sched sched; | |
978 | ||
cc1790cf | 979 | perf_sched_init(&sched, constraints, n, wmin, wmax, gpmax); |
1e2ad28f RR |
980 | |
981 | do { | |
982 | if (!perf_sched_find_counter(&sched)) | |
983 | break; /* failed */ | |
984 | if (assign) | |
985 | assign[sched.state.event] = sched.state.counter; | |
986 | } while (perf_sched_next_event(&sched)); | |
987 | ||
988 | return sched.state.unassigned; | |
989 | } | |
4a3dc121 | 990 | EXPORT_SYMBOL_GPL(perf_assign_events); |
1e2ad28f | 991 | |
de0428a7 | 992 | int x86_schedule_events(struct cpu_hw_events *cpuc, int n, int *assign) |
1da53e02 | 993 | { |
43b45780 | 994 | struct event_constraint *c; |
2f7f73a5 | 995 | struct perf_event *e; |
f80deefa | 996 | int n0, i, wmin, wmax, unsched = 0; |
1da53e02 | 997 | struct hw_perf_event *hwc; |
57388912 | 998 | u64 used_mask = 0; |
1da53e02 | 999 | |
f80deefa PZ |
1000 | /* |
1001 | * Compute the number of events already present; see x86_pmu_add(), | |
1002 | * validate_group() and x86_pmu_commit_txn(). For the former two | |
1003 | * cpuc->n_events hasn't been updated yet, while for the latter | |
1004 | * cpuc->n_txn contains the number of events added in the current | |
1005 | * transaction. | |
1006 | */ | |
1007 | n0 = cpuc->n_events; | |
1008 | if (cpuc->txn_flags & PERF_PMU_TXN_ADD) | |
1009 | n0 -= cpuc->n_txn; | |
1010 | ||
7c9903c9 | 1011 | static_call_cond(x86_pmu_start_scheduling)(cpuc); |
c5362c0c | 1012 | |
1e2ad28f | 1013 | for (i = 0, wmin = X86_PMC_IDX_MAX, wmax = 0; i < n; i++) { |
109717de PZ |
1014 | c = cpuc->event_constraint[i]; |
1015 | ||
f80deefa PZ |
1016 | /* |
1017 | * Previously scheduled events should have a cached constraint, | |
1018 | * while new events should not have one. | |
1019 | */ | |
1020 | WARN_ON_ONCE((c && i >= n0) || (!c && i < n0)); | |
1021 | ||
109717de PZ |
1022 | /* |
1023 | * Request constraints for new events; or for those events that | |
1024 | * have a dynamic constraint -- for those the constraint can | |
1025 | * change due to external factors (sibling state, allow_tfa). | |
1026 | */ | |
1027 | if (!c || (c->flags & PERF_X86_EVENT_DYNAMIC)) { | |
7c9903c9 | 1028 | c = static_call(x86_pmu_get_event_constraints)(cpuc, i, cpuc->event_list[i]); |
109717de PZ |
1029 | cpuc->event_constraint[i] = c; |
1030 | } | |
43b45780 | 1031 | |
1e2ad28f RR |
1032 | wmin = min(wmin, c->weight); |
1033 | wmax = max(wmax, c->weight); | |
1da53e02 SE |
1034 | } |
1035 | ||
8113070d SE |
1036 | /* |
1037 | * fastpath, try to reuse previous register | |
1038 | */ | |
c933c1a6 | 1039 | for (i = 0; i < n; i++) { |
57388912 KP |
1040 | u64 mask; |
1041 | ||
8113070d | 1042 | hwc = &cpuc->event_list[i]->hw; |
b371b594 | 1043 | c = cpuc->event_constraint[i]; |
8113070d SE |
1044 | |
1045 | /* never assigned */ | |
1046 | if (hwc->idx == -1) | |
1047 | break; | |
1048 | ||
1049 | /* constraint still honored */ | |
63b14649 | 1050 | if (!test_bit(hwc->idx, c->idxmsk)) |
8113070d SE |
1051 | break; |
1052 | ||
57388912 KP |
1053 | mask = BIT_ULL(hwc->idx); |
1054 | if (is_counter_pair(hwc)) | |
1055 | mask |= mask << 1; | |
1056 | ||
8113070d | 1057 | /* not already used */ |
57388912 | 1058 | if (used_mask & mask) |
8113070d SE |
1059 | break; |
1060 | ||
57388912 KP |
1061 | used_mask |= mask; |
1062 | ||
8113070d SE |
1063 | if (assign) |
1064 | assign[i] = hwc->idx; | |
1065 | } | |
8113070d | 1066 | |
1e2ad28f | 1067 | /* slow path */ |
b371b594 | 1068 | if (i != n) { |
722e42e4 | 1069 | int gpmax = x86_pmu_max_num_counters(cpuc->pmu); |
cc1790cf PZ |
1070 | |
1071 | /* | |
1072 | * Do not allow scheduling of more than half the available | |
1073 | * generic counters. | |
1074 | * | |
1075 | * This helps avoid counter starvation of sibling thread by | |
1076 | * ensuring at most half the counters cannot be in exclusive | |
1077 | * mode. There is no designated counters for the limits. Any | |
1078 | * N/2 counters can be used. This helps with events with | |
1079 | * specific counter constraints. | |
1080 | */ | |
1081 | if (is_ht_workaround_enabled() && !cpuc->is_fake && | |
1082 | READ_ONCE(cpuc->excl_cntrs->exclusive_present)) | |
1083 | gpmax /= 2; | |
1084 | ||
57388912 KP |
1085 | /* |
1086 | * Reduce the amount of available counters to allow fitting | |
1087 | * the extra Merge events needed by large increment events. | |
1088 | */ | |
1089 | if (x86_pmu.flags & PMU_FL_PAIR) { | |
722e42e4 | 1090 | gpmax -= cpuc->n_pair; |
57388912 KP |
1091 | WARN_ON(gpmax <= 0); |
1092 | } | |
1093 | ||
b371b594 | 1094 | unsched = perf_assign_events(cpuc->event_constraint, n, wmin, |
cc1790cf | 1095 | wmax, gpmax, assign); |
b371b594 | 1096 | } |
8113070d | 1097 | |
2f7f73a5 | 1098 | /* |
e979121b MD |
1099 | * In case of success (unsched = 0), mark events as committed, |
1100 | * so we do not put_constraint() in case new events are added | |
1101 | * and fail to be scheduled | |
1102 | * | |
1103 | * We invoke the lower level commit callback to lock the resource | |
1104 | * | |
1105 | * We do not need to do all of this in case we are called to | |
1106 | * validate an event group (assign == NULL) | |
2f7f73a5 | 1107 | */ |
e979121b | 1108 | if (!unsched && assign) { |
4f32da76 | 1109 | for (i = 0; i < n; i++) |
7c9903c9 | 1110 | static_call_cond(x86_pmu_commit_scheduling)(cpuc, i, assign[i]); |
8736e548 | 1111 | } else { |
f80deefa | 1112 | for (i = n0; i < n; i++) { |
2f7f73a5 | 1113 | e = cpuc->event_list[i]; |
2f7f73a5 | 1114 | |
e979121b MD |
1115 | /* |
1116 | * release events that failed scheduling | |
1117 | */ | |
7c9903c9 | 1118 | static_call_cond(x86_pmu_put_event_constraints)(cpuc, e); |
2c9651c3 PZ |
1119 | |
1120 | cpuc->event_constraint[i] = NULL; | |
1da53e02 SE |
1121 | } |
1122 | } | |
c5362c0c | 1123 | |
7c9903c9 | 1124 | static_call_cond(x86_pmu_stop_scheduling)(cpuc); |
c5362c0c | 1125 | |
e979121b | 1126 | return unsched ? -EINVAL : 0; |
1da53e02 SE |
1127 | } |
1128 | ||
7b2c05a1 KL |
1129 | static int add_nr_metric_event(struct cpu_hw_events *cpuc, |
1130 | struct perf_event *event) | |
1131 | { | |
1132 | if (is_metric_event(event)) { | |
1133 | if (cpuc->n_metric == INTEL_TD_METRIC_NUM) | |
1134 | return -EINVAL; | |
1135 | cpuc->n_metric++; | |
3dbde695 | 1136 | cpuc->n_txn_metric++; |
7b2c05a1 KL |
1137 | } |
1138 | ||
1139 | return 0; | |
1140 | } | |
1141 | ||
1142 | static void del_nr_metric_event(struct cpu_hw_events *cpuc, | |
1143 | struct perf_event *event) | |
1144 | { | |
1145 | if (is_metric_event(event)) | |
1146 | cpuc->n_metric--; | |
1147 | } | |
1148 | ||
1149 | static int collect_event(struct cpu_hw_events *cpuc, struct perf_event *event, | |
1150 | int max_count, int n) | |
1151 | { | |
d0946a88 | 1152 | union perf_capabilities intel_cap = hybrid(cpuc->pmu, intel_cap); |
7b2c05a1 | 1153 | |
d0946a88 | 1154 | if (intel_cap.perf_metrics && add_nr_metric_event(cpuc, event)) |
7b2c05a1 KL |
1155 | return -EINVAL; |
1156 | ||
1157 | if (n >= max_count + cpuc->n_metric) | |
1158 | return -EINVAL; | |
1159 | ||
1160 | cpuc->event_list[n] = event; | |
871a93b0 | 1161 | if (is_counter_pair(&event->hw)) { |
7b2c05a1 | 1162 | cpuc->n_pair++; |
871a93b0 PZ |
1163 | cpuc->n_txn_pair++; |
1164 | } | |
7b2c05a1 KL |
1165 | |
1166 | return 0; | |
1167 | } | |
1168 | ||
1da53e02 SE |
1169 | /* |
1170 | * dogrp: true if must collect siblings events (group) | |
1171 | * returns total number of events and error code | |
1172 | */ | |
1173 | static int collect_events(struct cpu_hw_events *cpuc, struct perf_event *leader, bool dogrp) | |
1174 | { | |
1175 | struct perf_event *event; | |
1176 | int n, max_count; | |
1177 | ||
722e42e4 | 1178 | max_count = x86_pmu_num_counters(cpuc->pmu) + x86_pmu_num_counters_fixed(cpuc->pmu); |
1da53e02 SE |
1179 | |
1180 | /* current number of events already accepted */ | |
1181 | n = cpuc->n_events; | |
42880f72 AS |
1182 | if (!cpuc->n_events) |
1183 | cpuc->pebs_output = 0; | |
1184 | ||
1185 | if (!cpuc->is_fake && leader->attr.precise_ip) { | |
1186 | /* | |
1187 | * For PEBS->PT, if !aux_event, the group leader (PT) went | |
1188 | * away, the group was broken down and this singleton event | |
1189 | * can't schedule any more. | |
1190 | */ | |
1191 | if (is_pebs_pt(leader) && !leader->aux_event) | |
1192 | return -EINVAL; | |
1193 | ||
1194 | /* | |
1195 | * pebs_output: 0: no PEBS so far, 1: PT, 2: DS | |
1196 | */ | |
1197 | if (cpuc->pebs_output && | |
1198 | cpuc->pebs_output != is_pebs_pt(leader) + 1) | |
1199 | return -EINVAL; | |
1200 | ||
1201 | cpuc->pebs_output = is_pebs_pt(leader) + 1; | |
1202 | } | |
1da53e02 SE |
1203 | |
1204 | if (is_x86_event(leader)) { | |
7b2c05a1 | 1205 | if (collect_event(cpuc, leader, max_count, n)) |
aa2bc1ad | 1206 | return -EINVAL; |
1da53e02 SE |
1207 | n++; |
1208 | } | |
7b2c05a1 | 1209 | |
1da53e02 SE |
1210 | if (!dogrp) |
1211 | return n; | |
1212 | ||
edb39592 | 1213 | for_each_sibling_event(event, leader) { |
7b2c05a1 | 1214 | if (!is_x86_event(event) || event->state <= PERF_EVENT_STATE_OFF) |
1da53e02 SE |
1215 | continue; |
1216 | ||
7b2c05a1 | 1217 | if (collect_event(cpuc, event, max_count, n)) |
aa2bc1ad | 1218 | return -EINVAL; |
1da53e02 | 1219 | |
1da53e02 SE |
1220 | n++; |
1221 | } | |
1222 | return n; | |
1223 | } | |
1224 | ||
1da53e02 | 1225 | static inline void x86_assign_hw_event(struct perf_event *event, |
447a194b | 1226 | struct cpu_hw_events *cpuc, int i) |
1da53e02 | 1227 | { |
447a194b | 1228 | struct hw_perf_event *hwc = &event->hw; |
027440b5 | 1229 | int idx; |
447a194b | 1230 | |
027440b5 | 1231 | idx = hwc->idx = cpuc->assign[i]; |
447a194b SE |
1232 | hwc->last_cpu = smp_processor_id(); |
1233 | hwc->last_tag = ++cpuc->tags[i]; | |
1da53e02 | 1234 | |
8b8ff8cc AH |
1235 | static_call_cond(x86_pmu_assign)(event, idx); |
1236 | ||
027440b5 LX |
1237 | switch (hwc->idx) { |
1238 | case INTEL_PMC_IDX_FIXED_BTS: | |
097e4311 | 1239 | case INTEL_PMC_IDX_FIXED_VLBR: |
1da53e02 SE |
1240 | hwc->config_base = 0; |
1241 | hwc->event_base = 0; | |
027440b5 LX |
1242 | break; |
1243 | ||
7b2c05a1 KL |
1244 | case INTEL_PMC_IDX_METRIC_BASE ... INTEL_PMC_IDX_METRIC_END: |
1245 | /* All the metric events are mapped onto the fixed counter 3. */ | |
1246 | idx = INTEL_PMC_IDX_FIXED_SLOTS; | |
b6459575 | 1247 | fallthrough; |
027440b5 | 1248 | case INTEL_PMC_IDX_FIXED ... INTEL_PMC_IDX_FIXED_BTS-1: |
1da53e02 | 1249 | hwc->config_base = MSR_ARCH_PERFMON_FIXED_CTR_CTRL; |
149fd471 | 1250 | hwc->event_base = x86_pmu_fixed_ctr_addr(idx - INTEL_PMC_IDX_FIXED); |
0e2e45e2 KL |
1251 | hwc->event_base_rdpmc = (idx - INTEL_PMC_IDX_FIXED) | |
1252 | INTEL_PMC_FIXED_RDPMC_BASE; | |
027440b5 LX |
1253 | break; |
1254 | ||
1255 | default: | |
73d6e522 RR |
1256 | hwc->config_base = x86_pmu_config_addr(hwc->idx); |
1257 | hwc->event_base = x86_pmu_event_addr(hwc->idx); | |
0fbdad07 | 1258 | hwc->event_base_rdpmc = x86_pmu_rdpmc_index(hwc->idx); |
027440b5 | 1259 | break; |
1da53e02 SE |
1260 | } |
1261 | } | |
1262 | ||
1182a495 RC |
1263 | /** |
1264 | * x86_perf_rdpmc_index - Return PMC counter used for event | |
1265 | * @event: the perf_event to which the PMC counter was assigned | |
1266 | * | |
1267 | * The counter assigned to this performance event may change if interrupts | |
1268 | * are enabled. This counter should thus never be used while interrupts are | |
1269 | * enabled. Before this function is used to obtain the assigned counter the | |
1270 | * event should be checked for validity using, for example, | |
1271 | * perf_event_read_local(), within the same interrupt disabled section in | |
1272 | * which this counter is planned to be used. | |
1273 | * | |
1274 | * Return: The index of the performance monitoring counter assigned to | |
1275 | * @perf_event. | |
1276 | */ | |
1277 | int x86_perf_rdpmc_index(struct perf_event *event) | |
1278 | { | |
1279 | lockdep_assert_irqs_disabled(); | |
1280 | ||
1281 | return event->hw.event_base_rdpmc; | |
1282 | } | |
1283 | ||
447a194b SE |
1284 | static inline int match_prev_assignment(struct hw_perf_event *hwc, |
1285 | struct cpu_hw_events *cpuc, | |
1286 | int i) | |
1287 | { | |
1288 | return hwc->idx == cpuc->assign[i] && | |
1289 | hwc->last_cpu == smp_processor_id() && | |
1290 | hwc->last_tag == cpuc->tags[i]; | |
1291 | } | |
1292 | ||
a4eaf7f1 | 1293 | static void x86_pmu_start(struct perf_event *event, int flags); |
2e841873 | 1294 | |
a4eaf7f1 | 1295 | static void x86_pmu_enable(struct pmu *pmu) |
ee06094f | 1296 | { |
89cbc767 | 1297 | struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); |
1da53e02 SE |
1298 | struct perf_event *event; |
1299 | struct hw_perf_event *hwc; | |
11164cd4 | 1300 | int i, added = cpuc->n_added; |
1da53e02 | 1301 | |
85cf9dba | 1302 | if (!x86_pmu_initialized()) |
2b9ff0db | 1303 | return; |
1a6e21f7 PZ |
1304 | |
1305 | if (cpuc->enabled) | |
1306 | return; | |
1307 | ||
1da53e02 | 1308 | if (cpuc->n_added) { |
19925ce7 | 1309 | int n_running = cpuc->n_events - cpuc->n_added; |
e02e9b03 KL |
1310 | |
1311 | /* | |
1312 | * The late setup (after counters are scheduled) | |
1313 | * is required for some cases, e.g., PEBS counters | |
1314 | * snapshotting. Because an accurate counter index | |
1315 | * is needed. | |
1316 | */ | |
1317 | static_call_cond(x86_pmu_late_setup)(); | |
1318 | ||
1da53e02 SE |
1319 | /* |
1320 | * apply assignment obtained either from | |
1321 | * hw_perf_group_sched_in() or x86_pmu_enable() | |
1322 | * | |
1323 | * step1: save events moving to new counters | |
1da53e02 | 1324 | */ |
19925ce7 | 1325 | for (i = 0; i < n_running; i++) { |
1da53e02 SE |
1326 | event = cpuc->event_list[i]; |
1327 | hwc = &event->hw; | |
1328 | ||
447a194b SE |
1329 | /* |
1330 | * we can avoid reprogramming counter if: | |
1331 | * - assigned same counter as last time | |
1332 | * - running on same CPU as last time | |
1333 | * - no other event has used the counter since | |
1334 | */ | |
1335 | if (hwc->idx == -1 || | |
1336 | match_prev_assignment(hwc, cpuc, i)) | |
1da53e02 SE |
1337 | continue; |
1338 | ||
a4eaf7f1 PZ |
1339 | /* |
1340 | * Ensure we don't accidentally enable a stopped | |
1341 | * counter simply because we rescheduled. | |
1342 | */ | |
1343 | if (hwc->state & PERF_HES_STOPPED) | |
1344 | hwc->state |= PERF_HES_ARCH; | |
1345 | ||
1346 | x86_pmu_stop(event, PERF_EF_UPDATE); | |
1da53e02 SE |
1347 | } |
1348 | ||
c347a2f1 PZ |
1349 | /* |
1350 | * step2: reprogram moved events into new counters | |
1351 | */ | |
1da53e02 | 1352 | for (i = 0; i < cpuc->n_events; i++) { |
1da53e02 SE |
1353 | event = cpuc->event_list[i]; |
1354 | hwc = &event->hw; | |
1355 | ||
45e16a68 | 1356 | if (!match_prev_assignment(hwc, cpuc, i)) |
447a194b | 1357 | x86_assign_hw_event(event, cpuc, i); |
45e16a68 PZ |
1358 | else if (i < n_running) |
1359 | continue; | |
1da53e02 | 1360 | |
a4eaf7f1 PZ |
1361 | if (hwc->state & PERF_HES_ARCH) |
1362 | continue; | |
1363 | ||
ada54345 SE |
1364 | /* |
1365 | * if cpuc->enabled = 0, then no wrmsr as | |
1366 | * per x86_pmu_enable_event() | |
1367 | */ | |
a4eaf7f1 | 1368 | x86_pmu_start(event, PERF_EF_RELOAD); |
1da53e02 SE |
1369 | } |
1370 | cpuc->n_added = 0; | |
1371 | perf_events_lapic_init(); | |
1372 | } | |
1a6e21f7 PZ |
1373 | |
1374 | cpuc->enabled = 1; | |
1375 | barrier(); | |
1376 | ||
7c9903c9 | 1377 | static_call(x86_pmu_enable_all)(added); |
ee06094f | 1378 | } |
ee06094f | 1379 | |
dbf4e792 | 1380 | DEFINE_PER_CPU(u64 [X86_PMC_IDX_MAX], pmc_prev_left); |
241771ef | 1381 | |
ee06094f IM |
1382 | /* |
1383 | * Set the next IRQ period, based on the hwc->period_left value. | |
cdd6c482 | 1384 | * To be called with the event disabled in hw: |
ee06094f | 1385 | */ |
de0428a7 | 1386 | int x86_perf_event_set_period(struct perf_event *event) |
241771ef | 1387 | { |
07088edb | 1388 | struct hw_perf_event *hwc = &event->hw; |
e7850595 | 1389 | s64 left = local64_read(&hwc->period_left); |
e4abb5d4 | 1390 | s64 period = hwc->sample_period; |
7645a24c | 1391 | int ret = 0, idx = hwc->idx; |
ee06094f | 1392 | |
027440b5 | 1393 | if (unlikely(!hwc->event_base)) |
30dd568c MM |
1394 | return 0; |
1395 | ||
ee06094f | 1396 | /* |
af901ca1 | 1397 | * If we are way outside a reasonable range then just skip forward: |
ee06094f IM |
1398 | */ |
1399 | if (unlikely(left <= -period)) { | |
1400 | left = period; | |
e7850595 | 1401 | local64_set(&hwc->period_left, left); |
9e350de3 | 1402 | hwc->last_period = period; |
e4abb5d4 | 1403 | ret = 1; |
ee06094f IM |
1404 | } |
1405 | ||
1406 | if (unlikely(left <= 0)) { | |
1407 | left += period; | |
e7850595 | 1408 | local64_set(&hwc->period_left, left); |
9e350de3 | 1409 | hwc->last_period = period; |
e4abb5d4 | 1410 | ret = 1; |
ee06094f | 1411 | } |
1c80f4b5 | 1412 | /* |
dfc65094 | 1413 | * Quirk: certain CPUs dont like it if just 1 hw_event is left: |
1c80f4b5 IM |
1414 | */ |
1415 | if (unlikely(left < 2)) | |
1416 | left = 2; | |
241771ef | 1417 | |
e4abb5d4 PZ |
1418 | if (left > x86_pmu.max_period) |
1419 | left = x86_pmu.max_period; | |
1420 | ||
08b3068f | 1421 | static_call_cond(x86_pmu_limit_period)(event, &left); |
294fe0f5 | 1422 | |
28f0f3c4 | 1423 | this_cpu_write(pmc_prev_left[idx], left); |
ee06094f | 1424 | |
d31fc13f KL |
1425 | /* |
1426 | * The hw event starts counting from this event offset, | |
1427 | * mark it to be able to extra future deltas: | |
1428 | */ | |
1429 | local64_set(&hwc->prev_count, (u64)-left); | |
ee06094f | 1430 | |
78255eb2 | 1431 | wrmsrq(hwc->event_base, (u64)(-left) & x86_pmu.cntval_mask); |
68aa00ac | 1432 | |
57388912 | 1433 | /* |
26e52558 | 1434 | * Sign extend the Merge event counter's upper 16 bits since |
57388912 KP |
1435 | * we currently declare a 48-bit counter width |
1436 | */ | |
1437 | if (is_counter_pair(hwc)) | |
78255eb2 | 1438 | wrmsrq(x86_pmu_event_addr(idx + 1), 0xffff); |
57388912 | 1439 | |
cdd6c482 | 1440 | perf_event_update_userpage(event); |
194002b2 | 1441 | |
e4abb5d4 | 1442 | return ret; |
2f18d1e8 IM |
1443 | } |
1444 | ||
de0428a7 | 1445 | void x86_pmu_enable_event(struct perf_event *event) |
7c90cc45 | 1446 | { |
0a3aee0d | 1447 | if (__this_cpu_read(cpu_hw_events.enabled)) |
31fa58af RR |
1448 | __x86_pmu_enable_event(&event->hw, |
1449 | ARCH_PERFMON_EVENTSEL_ENABLE); | |
241771ef IM |
1450 | } |
1451 | ||
b690081d | 1452 | /* |
a4eaf7f1 | 1453 | * Add a single event to the PMU. |
1da53e02 SE |
1454 | * |
1455 | * The event is added to the group of enabled events | |
77d76032 | 1456 | * but only if it can be scheduled with existing events. |
fe9081cc | 1457 | */ |
a4eaf7f1 | 1458 | static int x86_pmu_add(struct perf_event *event, int flags) |
fe9081cc | 1459 | { |
89cbc767 | 1460 | struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); |
1da53e02 SE |
1461 | struct hw_perf_event *hwc; |
1462 | int assign[X86_PMC_IDX_MAX]; | |
1463 | int n, n0, ret; | |
fe9081cc | 1464 | |
1da53e02 | 1465 | hwc = &event->hw; |
fe9081cc | 1466 | |
1da53e02 | 1467 | n0 = cpuc->n_events; |
24cd7f54 PZ |
1468 | ret = n = collect_events(cpuc, event, false); |
1469 | if (ret < 0) | |
1470 | goto out; | |
53b441a5 | 1471 | |
a4eaf7f1 PZ |
1472 | hwc->state = PERF_HES_UPTODATE | PERF_HES_STOPPED; |
1473 | if (!(flags & PERF_EF_START)) | |
1474 | hwc->state |= PERF_HES_ARCH; | |
1475 | ||
4d1c52b0 LM |
1476 | /* |
1477 | * If group events scheduling transaction was started, | |
0d2eb44f | 1478 | * skip the schedulability test here, it will be performed |
c347a2f1 | 1479 | * at commit time (->commit_txn) as a whole. |
68f7082f PZ |
1480 | * |
1481 | * If commit fails, we'll call ->del() on all events | |
1482 | * for which ->add() was called. | |
4d1c52b0 | 1483 | */ |
8f3e5684 | 1484 | if (cpuc->txn_flags & PERF_PMU_TXN_ADD) |
24cd7f54 | 1485 | goto done_collect; |
4d1c52b0 | 1486 | |
7c9903c9 | 1487 | ret = static_call(x86_pmu_schedule_events)(cpuc, n, assign); |
1da53e02 | 1488 | if (ret) |
24cd7f54 | 1489 | goto out; |
1da53e02 SE |
1490 | /* |
1491 | * copy new assignment, now we know it is possible | |
1492 | * will be used by hw_perf_enable() | |
1493 | */ | |
1494 | memcpy(cpuc->assign, assign, n*sizeof(int)); | |
7e2ae347 | 1495 | |
24cd7f54 | 1496 | done_collect: |
c347a2f1 PZ |
1497 | /* |
1498 | * Commit the collect_events() state. See x86_pmu_del() and | |
1499 | * x86_pmu_*_txn(). | |
1500 | */ | |
1da53e02 | 1501 | cpuc->n_events = n; |
356e1f2e | 1502 | cpuc->n_added += n - n0; |
90151c35 | 1503 | cpuc->n_txn += n - n0; |
95cdd2e7 | 1504 | |
7c9903c9 PZ |
1505 | /* |
1506 | * This is before x86_pmu_enable() will call x86_pmu_start(), | |
1507 | * so we enable LBRs before an event needs them etc.. | |
1508 | */ | |
1509 | static_call_cond(x86_pmu_add)(event); | |
68f7082f | 1510 | |
24cd7f54 PZ |
1511 | ret = 0; |
1512 | out: | |
24cd7f54 | 1513 | return ret; |
241771ef IM |
1514 | } |
1515 | ||
a4eaf7f1 | 1516 | static void x86_pmu_start(struct perf_event *event, int flags) |
d76a0812 | 1517 | { |
89cbc767 | 1518 | struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); |
c08053e6 PZ |
1519 | int idx = event->hw.idx; |
1520 | ||
a4eaf7f1 PZ |
1521 | if (WARN_ON_ONCE(!(event->hw.state & PERF_HES_STOPPED))) |
1522 | return; | |
1523 | ||
1524 | if (WARN_ON_ONCE(idx == -1)) | |
1525 | return; | |
1526 | ||
1527 | if (flags & PERF_EF_RELOAD) { | |
1528 | WARN_ON_ONCE(!(event->hw.state & PERF_HES_UPTODATE)); | |
73759c34 | 1529 | static_call(x86_pmu_set_period)(event); |
a4eaf7f1 PZ |
1530 | } |
1531 | ||
1532 | event->hw.state = 0; | |
d76a0812 | 1533 | |
c08053e6 PZ |
1534 | cpuc->events[idx] = event; |
1535 | __set_bit(idx, cpuc->active_mask); | |
7c9903c9 | 1536 | static_call(x86_pmu_enable)(event); |
c08053e6 | 1537 | perf_event_update_userpage(event); |
a78ac325 PZ |
1538 | } |
1539 | ||
cdd6c482 | 1540 | void perf_event_print_debug(void) |
241771ef | 1541 | { |
2f18d1e8 | 1542 | u64 ctrl, status, overflow, pmc_ctrl, pmc_count, prev_left, fixed; |
f73cefa3 LH |
1543 | unsigned long *cntr_mask, *fixed_cntr_mask; |
1544 | struct event_constraint *pebs_constraints; | |
1545 | struct cpu_hw_events *cpuc; | |
da3e606d | 1546 | u64 pebs, debugctl; |
f73cefa3 LH |
1547 | int cpu, idx; |
1548 | ||
1549 | guard(irqsave)(); | |
1550 | ||
1551 | cpu = smp_processor_id(); | |
1552 | cpuc = &per_cpu(cpu_hw_events, cpu); | |
1553 | cntr_mask = hybrid(cpuc->pmu, cntr_mask); | |
1554 | fixed_cntr_mask = hybrid(cpuc->pmu, fixed_cntr_mask); | |
1555 | pebs_constraints = hybrid(cpuc->pmu, pebs_constraints); | |
1e125676 | 1556 | |
722e42e4 | 1557 | if (!*(u64 *)cntr_mask) |
1e125676 | 1558 | return; |
241771ef | 1559 | |
faa28ae0 | 1560 | if (x86_pmu.version >= 2) { |
c435e608 IM |
1561 | rdmsrq(MSR_CORE_PERF_GLOBAL_CTRL, ctrl); |
1562 | rdmsrq(MSR_CORE_PERF_GLOBAL_STATUS, status); | |
1563 | rdmsrq(MSR_CORE_PERF_GLOBAL_OVF_CTRL, overflow); | |
1564 | rdmsrq(MSR_ARCH_PERFMON_FIXED_CTR_CTRL, fixed); | |
a1ef58f4 JSR |
1565 | |
1566 | pr_info("\n"); | |
1567 | pr_info("CPU#%d: ctrl: %016llx\n", cpu, ctrl); | |
1568 | pr_info("CPU#%d: status: %016llx\n", cpu, status); | |
1569 | pr_info("CPU#%d: overflow: %016llx\n", cpu, overflow); | |
1570 | pr_info("CPU#%d: fixed: %016llx\n", cpu, fixed); | |
24ee38ff | 1571 | if (pebs_constraints) { |
c435e608 | 1572 | rdmsrq(MSR_IA32_PEBS_ENABLE, pebs); |
15fde110 AK |
1573 | pr_info("CPU#%d: pebs: %016llx\n", cpu, pebs); |
1574 | } | |
da3e606d | 1575 | if (x86_pmu.lbr_nr) { |
c435e608 | 1576 | rdmsrq(MSR_IA32_DEBUGCTLMSR, debugctl); |
da3e606d AK |
1577 | pr_info("CPU#%d: debugctl: %016llx\n", cpu, debugctl); |
1578 | } | |
f87ad35d | 1579 | } |
7645a24c | 1580 | pr_info("CPU#%d: active: %016llx\n", cpu, *(u64 *)cpuc->active_mask); |
241771ef | 1581 | |
722e42e4 | 1582 | for_each_set_bit(idx, cntr_mask, X86_PMC_IDX_MAX) { |
c435e608 IM |
1583 | rdmsrq(x86_pmu_config_addr(idx), pmc_ctrl); |
1584 | rdmsrq(x86_pmu_event_addr(idx), pmc_count); | |
241771ef | 1585 | |
245b2e70 | 1586 | prev_left = per_cpu(pmc_prev_left[idx], cpu); |
241771ef | 1587 | |
a1ef58f4 | 1588 | pr_info("CPU#%d: gen-PMC%d ctrl: %016llx\n", |
241771ef | 1589 | cpu, idx, pmc_ctrl); |
a1ef58f4 | 1590 | pr_info("CPU#%d: gen-PMC%d count: %016llx\n", |
241771ef | 1591 | cpu, idx, pmc_count); |
a1ef58f4 | 1592 | pr_info("CPU#%d: gen-PMC%d left: %016llx\n", |
ee06094f | 1593 | cpu, idx, prev_left); |
241771ef | 1594 | } |
722e42e4 | 1595 | for_each_set_bit(idx, fixed_cntr_mask, X86_PMC_IDX_MAX) { |
fc4b8fca | 1596 | if (fixed_counter_disabled(idx, cpuc->pmu)) |
32451614 | 1597 | continue; |
c435e608 | 1598 | rdmsrq(x86_pmu_fixed_ctr_addr(idx), pmc_count); |
2f18d1e8 | 1599 | |
a1ef58f4 | 1600 | pr_info("CPU#%d: fixed-PMC%d count: %016llx\n", |
2f18d1e8 IM |
1601 | cpu, idx, pmc_count); |
1602 | } | |
241771ef IM |
1603 | } |
1604 | ||
de0428a7 | 1605 | void x86_pmu_stop(struct perf_event *event, int flags) |
241771ef | 1606 | { |
89cbc767 | 1607 | struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); |
cdd6c482 | 1608 | struct hw_perf_event *hwc = &event->hw; |
241771ef | 1609 | |
3966c3fe | 1610 | if (test_bit(hwc->idx, cpuc->active_mask)) { |
7c9903c9 | 1611 | static_call(x86_pmu_disable)(event); |
3966c3fe | 1612 | __clear_bit(hwc->idx, cpuc->active_mask); |
a4eaf7f1 PZ |
1613 | cpuc->events[hwc->idx] = NULL; |
1614 | WARN_ON_ONCE(hwc->state & PERF_HES_STOPPED); | |
1615 | hwc->state |= PERF_HES_STOPPED; | |
1616 | } | |
30dd568c | 1617 | |
a4eaf7f1 PZ |
1618 | if ((flags & PERF_EF_UPDATE) && !(hwc->state & PERF_HES_UPTODATE)) { |
1619 | /* | |
1620 | * Drain the remaining delta count out of a event | |
1621 | * that we are disabling: | |
1622 | */ | |
73759c34 | 1623 | static_call(x86_pmu_update)(event); |
a4eaf7f1 PZ |
1624 | hwc->state |= PERF_HES_UPTODATE; |
1625 | } | |
2e841873 PZ |
1626 | } |
1627 | ||
a4eaf7f1 | 1628 | static void x86_pmu_del(struct perf_event *event, int flags) |
2e841873 | 1629 | { |
89cbc767 | 1630 | struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); |
d0946a88 | 1631 | union perf_capabilities intel_cap = hybrid(cpuc->pmu, intel_cap); |
2e841873 PZ |
1632 | int i; |
1633 | ||
90151c35 | 1634 | /* |
68f7082f | 1635 | * If we're called during a txn, we only need to undo x86_pmu.add. |
90151c35 SE |
1636 | * The events never got scheduled and ->cancel_txn will truncate |
1637 | * the event_list. | |
c347a2f1 PZ |
1638 | * |
1639 | * XXX assumes any ->del() called during a TXN will only be on | |
1640 | * an event added during that same TXN. | |
90151c35 | 1641 | */ |
8f3e5684 | 1642 | if (cpuc->txn_flags & PERF_PMU_TXN_ADD) |
68f7082f | 1643 | goto do_del; |
90151c35 | 1644 | |
5471eea5 KL |
1645 | __set_bit(event->hw.idx, cpuc->dirty); |
1646 | ||
c347a2f1 PZ |
1647 | /* |
1648 | * Not a TXN, therefore cleanup properly. | |
1649 | */ | |
a4eaf7f1 | 1650 | x86_pmu_stop(event, PERF_EF_UPDATE); |
194002b2 | 1651 | |
1da53e02 | 1652 | for (i = 0; i < cpuc->n_events; i++) { |
c347a2f1 PZ |
1653 | if (event == cpuc->event_list[i]) |
1654 | break; | |
1655 | } | |
1da53e02 | 1656 | |
c347a2f1 PZ |
1657 | if (WARN_ON_ONCE(i == cpuc->n_events)) /* called ->del() without ->add() ? */ |
1658 | return; | |
26e61e89 | 1659 | |
c347a2f1 PZ |
1660 | /* If we have a newly added event; make sure to decrease n_added. */ |
1661 | if (i >= cpuc->n_events - cpuc->n_added) | |
1662 | --cpuc->n_added; | |
1da53e02 | 1663 | |
7c9903c9 | 1664 | static_call_cond(x86_pmu_put_event_constraints)(cpuc, event); |
c347a2f1 PZ |
1665 | |
1666 | /* Delete the array entry. */ | |
b371b594 | 1667 | while (++i < cpuc->n_events) { |
c347a2f1 | 1668 | cpuc->event_list[i-1] = cpuc->event_list[i]; |
b371b594 | 1669 | cpuc->event_constraint[i-1] = cpuc->event_constraint[i]; |
dec8ced8 | 1670 | cpuc->assign[i-1] = cpuc->assign[i]; |
b371b594 | 1671 | } |
2c9651c3 | 1672 | cpuc->event_constraint[i-1] = NULL; |
c347a2f1 | 1673 | --cpuc->n_events; |
d0946a88 | 1674 | if (intel_cap.perf_metrics) |
7b2c05a1 | 1675 | del_nr_metric_event(cpuc, event); |
1da53e02 | 1676 | |
cdd6c482 | 1677 | perf_event_update_userpage(event); |
68f7082f PZ |
1678 | |
1679 | do_del: | |
7c9903c9 PZ |
1680 | |
1681 | /* | |
1682 | * This is after x86_pmu_stop(); so we disable LBRs after any | |
1683 | * event can need them etc.. | |
1684 | */ | |
1685 | static_call_cond(x86_pmu_del)(event); | |
241771ef IM |
1686 | } |
1687 | ||
de0428a7 | 1688 | int x86_pmu_handle_irq(struct pt_regs *regs) |
a29aa8a7 | 1689 | { |
df1a132b | 1690 | struct perf_sample_data data; |
cdd6c482 IM |
1691 | struct cpu_hw_events *cpuc; |
1692 | struct perf_event *event; | |
11d1578f | 1693 | int idx, handled = 0; |
1734d98f | 1694 | u64 last_period; |
9029a5e3 IM |
1695 | u64 val; |
1696 | ||
89cbc767 | 1697 | cpuc = this_cpu_ptr(&cpu_hw_events); |
962bf7a6 | 1698 | |
2bce5dac DZ |
1699 | /* |
1700 | * Some chipsets need to unmask the LVTPC in a particular spot | |
1701 | * inside the nmi handler. As a result, the unmasking was pushed | |
1702 | * into all the nmi handlers. | |
1703 | * | |
1704 | * This generic handler doesn't seem to have any issues where the | |
1705 | * unmasking occurs so it was left at the top. | |
1706 | */ | |
1707 | apic_write(APIC_LVTPC, APIC_DM_NMI); | |
1708 | ||
722e42e4 | 1709 | for_each_set_bit(idx, x86_pmu.cntr_mask, X86_PMC_IDX_MAX) { |
3966c3fe | 1710 | if (!test_bit(idx, cpuc->active_mask)) |
a29aa8a7 | 1711 | continue; |
962bf7a6 | 1712 | |
cdd6c482 | 1713 | event = cpuc->events[idx]; |
1734d98f | 1714 | last_period = event->hw.last_period; |
a4016a79 | 1715 | |
73759c34 | 1716 | val = static_call(x86_pmu_update)(event); |
948b1bb8 | 1717 | if (val & (1ULL << (x86_pmu.cntval_bits - 1))) |
48e22d56 | 1718 | continue; |
962bf7a6 | 1719 | |
9e350de3 | 1720 | /* |
cdd6c482 | 1721 | * event overflow |
9e350de3 | 1722 | */ |
4177c42a | 1723 | handled++; |
9e350de3 | 1724 | |
73759c34 | 1725 | if (!static_call(x86_pmu_set_period)(event)) |
e4abb5d4 PZ |
1726 | continue; |
1727 | ||
1734d98f | 1728 | perf_sample_data_init(&data, 0, last_period); |
ada54345 | 1729 | |
faac6f10 | 1730 | perf_sample_save_brstack(&data, event, &cpuc->lbr_stack, NULL); |
ada54345 | 1731 | |
b8328f67 | 1732 | perf_event_overflow(event, &data, regs); |
a29aa8a7 | 1733 | } |
962bf7a6 | 1734 | |
9e350de3 PZ |
1735 | if (handled) |
1736 | inc_irq_stat(apic_perf_irqs); | |
1737 | ||
a29aa8a7 RR |
1738 | return handled; |
1739 | } | |
39d81eab | 1740 | |
cdd6c482 | 1741 | void perf_events_lapic_init(void) |
241771ef | 1742 | { |
04da8a43 | 1743 | if (!x86_pmu.apic || !x86_pmu_initialized()) |
241771ef | 1744 | return; |
85cf9dba | 1745 | |
241771ef | 1746 | /* |
c323d95f | 1747 | * Always use NMI for PMU |
241771ef | 1748 | */ |
c323d95f | 1749 | apic_write(APIC_LVTPC, APIC_DM_NMI); |
241771ef IM |
1750 | } |
1751 | ||
9326638c | 1752 | static int |
9c48f1c6 | 1753 | perf_event_nmi_handler(unsigned int cmd, struct pt_regs *regs) |
241771ef | 1754 | { |
14c63f17 DH |
1755 | u64 start_clock; |
1756 | u64 finish_clock; | |
e8a923cc | 1757 | int ret; |
14c63f17 | 1758 | |
1b7b938f AS |
1759 | /* |
1760 | * All PMUs/events that share this PMI handler should make sure to | |
1761 | * increment active_events for their events. | |
1762 | */ | |
cdd6c482 | 1763 | if (!atomic_read(&active_events)) |
9c48f1c6 | 1764 | return NMI_DONE; |
4177c42a | 1765 | |
e8a923cc | 1766 | start_clock = sched_clock(); |
7c9903c9 | 1767 | ret = static_call(x86_pmu_handle_irq)(regs); |
e8a923cc | 1768 | finish_clock = sched_clock(); |
14c63f17 DH |
1769 | |
1770 | perf_sample_event_took(finish_clock - start_clock); | |
1771 | ||
1772 | return ret; | |
241771ef | 1773 | } |
9326638c | 1774 | NOKPROBE_SYMBOL(perf_event_nmi_handler); |
241771ef | 1775 | |
de0428a7 KW |
1776 | struct event_constraint emptyconstraint; |
1777 | struct event_constraint unconstrained; | |
f87ad35d | 1778 | |
95ca792c | 1779 | static int x86_pmu_prepare_cpu(unsigned int cpu) |
3f6da390 | 1780 | { |
7fdba1ca | 1781 | struct cpu_hw_events *cpuc = &per_cpu(cpu_hw_events, cpu); |
95ca792c | 1782 | int i; |
3f6da390 | 1783 | |
95ca792c TG |
1784 | for (i = 0 ; i < X86_PERF_KFREE_MAX; i++) |
1785 | cpuc->kfree_on_online[i] = NULL; | |
1786 | if (x86_pmu.cpu_prepare) | |
1787 | return x86_pmu.cpu_prepare(cpu); | |
1788 | return 0; | |
1789 | } | |
7fdba1ca | 1790 | |
95ca792c TG |
1791 | static int x86_pmu_dead_cpu(unsigned int cpu) |
1792 | { | |
1793 | if (x86_pmu.cpu_dead) | |
1794 | x86_pmu.cpu_dead(cpu); | |
1795 | return 0; | |
1796 | } | |
3f6da390 | 1797 | |
95ca792c TG |
1798 | static int x86_pmu_online_cpu(unsigned int cpu) |
1799 | { | |
1800 | struct cpu_hw_events *cpuc = &per_cpu(cpu_hw_events, cpu); | |
1801 | int i; | |
3f6da390 | 1802 | |
95ca792c TG |
1803 | for (i = 0 ; i < X86_PERF_KFREE_MAX; i++) { |
1804 | kfree(cpuc->kfree_on_online[i]); | |
1805 | cpuc->kfree_on_online[i] = NULL; | |
3f6da390 | 1806 | } |
95ca792c TG |
1807 | return 0; |
1808 | } | |
3f6da390 | 1809 | |
95ca792c TG |
1810 | static int x86_pmu_starting_cpu(unsigned int cpu) |
1811 | { | |
1812 | if (x86_pmu.cpu_starting) | |
1813 | x86_pmu.cpu_starting(cpu); | |
1814 | return 0; | |
1815 | } | |
1816 | ||
1817 | static int x86_pmu_dying_cpu(unsigned int cpu) | |
1818 | { | |
1819 | if (x86_pmu.cpu_dying) | |
1820 | x86_pmu.cpu_dying(cpu); | |
1821 | return 0; | |
3f6da390 PZ |
1822 | } |
1823 | ||
12558038 CG |
1824 | static void __init pmu_check_apic(void) |
1825 | { | |
93984fbd | 1826 | if (boot_cpu_has(X86_FEATURE_APIC)) |
12558038 CG |
1827 | return; |
1828 | ||
1829 | x86_pmu.apic = 0; | |
1830 | pr_info("no APIC, boot with the \"lapic\" boot parameter to force-enable it.\n"); | |
1831 | pr_info("no hardware sampling interrupt available.\n"); | |
c184c980 VW |
1832 | |
1833 | /* | |
1834 | * If we have a PMU initialized but no APIC | |
1835 | * interrupts, we cannot sample hardware | |
1836 | * events (user-space has to fall back and | |
1837 | * sample via a hrtimer based software event): | |
1838 | */ | |
1839 | pmu.capabilities |= PERF_PMU_CAP_NO_INTERRUPT; | |
1840 | ||
12558038 CG |
1841 | } |
1842 | ||
2766d2ee | 1843 | static struct attribute_group x86_pmu_format_group __ro_after_init = { |
641cc938 JO |
1844 | .name = "format", |
1845 | .attrs = NULL, | |
1846 | }; | |
1847 | ||
c7ab62bf | 1848 | ssize_t events_sysfs_show(struct device *dev, struct device_attribute *attr, char *page) |
a4747393 | 1849 | { |
1e69a0ef | 1850 | struct perf_pmu_events_attr *pmu_attr = |
a4747393 | 1851 | container_of(attr, struct perf_pmu_events_attr, attr); |
1e69a0ef PZ |
1852 | u64 config = 0; |
1853 | ||
1854 | if (pmu_attr->id < x86_pmu.max_events) | |
1855 | config = x86_pmu.event_map(pmu_attr->id); | |
a4747393 | 1856 | |
3a54aaa0 SE |
1857 | /* string trumps id */ |
1858 | if (pmu_attr->event_str) | |
7bebfe9d | 1859 | return sprintf(page, "%s\n", pmu_attr->event_str); |
a4747393 | 1860 | |
3a54aaa0 SE |
1861 | return x86_pmu.events_sysfs_show(page, config); |
1862 | } | |
c7ab62bf | 1863 | EXPORT_SYMBOL_GPL(events_sysfs_show); |
a4747393 | 1864 | |
fc07e9f9 AK |
1865 | ssize_t events_ht_sysfs_show(struct device *dev, struct device_attribute *attr, |
1866 | char *page) | |
1867 | { | |
1868 | struct perf_pmu_events_ht_attr *pmu_attr = | |
1869 | container_of(attr, struct perf_pmu_events_ht_attr, attr); | |
1870 | ||
1871 | /* | |
1872 | * Report conditional events depending on Hyper-Threading. | |
1873 | * | |
1874 | * This is overly conservative as usually the HT special | |
1875 | * handling is not needed if the other CPU thread is idle. | |
1876 | * | |
1877 | * Note this does not (and cannot) handle the case when thread | |
1878 | * siblings are invisible, for example with virtualization | |
1879 | * if they are owned by some other guest. The user tool | |
1880 | * has to re-read when a thread sibling gets onlined later. | |
1881 | */ | |
1882 | return sprintf(page, "%s", | |
1883 | topology_max_smt_threads() > 1 ? | |
1884 | pmu_attr->event_str_ht : | |
1885 | pmu_attr->event_str_noht); | |
1886 | } | |
1887 | ||
a9c81ccd KL |
1888 | ssize_t events_hybrid_sysfs_show(struct device *dev, |
1889 | struct device_attribute *attr, | |
1890 | char *page) | |
1891 | { | |
1892 | struct perf_pmu_events_hybrid_attr *pmu_attr = | |
1893 | container_of(attr, struct perf_pmu_events_hybrid_attr, attr); | |
1894 | struct x86_hybrid_pmu *pmu; | |
1895 | const char *str, *next_str; | |
1896 | int i; | |
1897 | ||
1898 | if (hweight64(pmu_attr->pmu_type) == 1) | |
1899 | return sprintf(page, "%s", pmu_attr->event_str); | |
1900 | ||
1901 | /* | |
1902 | * Hybrid PMUs may support the same event name, but with different | |
1903 | * event encoding, e.g., the mem-loads event on an Atom PMU has | |
1904 | * different event encoding from a Core PMU. | |
1905 | * | |
1906 | * The event_str includes all event encodings. Each event encoding | |
1907 | * is divided by ";". The order of the event encodings must follow | |
1908 | * the order of the hybrid PMU index. | |
1909 | */ | |
1910 | pmu = container_of(dev_get_drvdata(dev), struct x86_hybrid_pmu, pmu); | |
1911 | ||
1912 | str = pmu_attr->event_str; | |
1913 | for (i = 0; i < x86_pmu.num_hybrid_pmus; i++) { | |
b0560bfd | 1914 | if (!(x86_pmu.hybrid_pmu[i].pmu_type & pmu_attr->pmu_type)) |
a9c81ccd | 1915 | continue; |
b0560bfd | 1916 | if (x86_pmu.hybrid_pmu[i].pmu_type & pmu->pmu_type) { |
a9c81ccd KL |
1917 | next_str = strchr(str, ';'); |
1918 | if (next_str) | |
1919 | return snprintf(page, next_str - str + 1, "%s", str); | |
1920 | else | |
1921 | return sprintf(page, "%s", str); | |
1922 | } | |
1923 | str = strchr(str, ';'); | |
1924 | str++; | |
1925 | } | |
1926 | ||
1927 | return 0; | |
1928 | } | |
1929 | EXPORT_SYMBOL_GPL(events_hybrid_sysfs_show); | |
1930 | ||
a4747393 JO |
1931 | EVENT_ATTR(cpu-cycles, CPU_CYCLES ); |
1932 | EVENT_ATTR(instructions, INSTRUCTIONS ); | |
1933 | EVENT_ATTR(cache-references, CACHE_REFERENCES ); | |
1934 | EVENT_ATTR(cache-misses, CACHE_MISSES ); | |
1935 | EVENT_ATTR(branch-instructions, BRANCH_INSTRUCTIONS ); | |
1936 | EVENT_ATTR(branch-misses, BRANCH_MISSES ); | |
1937 | EVENT_ATTR(bus-cycles, BUS_CYCLES ); | |
1938 | EVENT_ATTR(stalled-cycles-frontend, STALLED_CYCLES_FRONTEND ); | |
1939 | EVENT_ATTR(stalled-cycles-backend, STALLED_CYCLES_BACKEND ); | |
1940 | EVENT_ATTR(ref-cycles, REF_CPU_CYCLES ); | |
1941 | ||
1942 | static struct attribute *empty_attrs; | |
1943 | ||
95d18aa2 | 1944 | static struct attribute *events_attr[] = { |
a4747393 JO |
1945 | EVENT_PTR(CPU_CYCLES), |
1946 | EVENT_PTR(INSTRUCTIONS), | |
1947 | EVENT_PTR(CACHE_REFERENCES), | |
1948 | EVENT_PTR(CACHE_MISSES), | |
1949 | EVENT_PTR(BRANCH_INSTRUCTIONS), | |
1950 | EVENT_PTR(BRANCH_MISSES), | |
1951 | EVENT_PTR(BUS_CYCLES), | |
1952 | EVENT_PTR(STALLED_CYCLES_FRONTEND), | |
1953 | EVENT_PTR(STALLED_CYCLES_BACKEND), | |
1954 | EVENT_PTR(REF_CPU_CYCLES), | |
1955 | NULL, | |
1956 | }; | |
1957 | ||
3d567273 JO |
1958 | /* |
1959 | * Remove all undefined events (x86_pmu.event_map(id) == 0) | |
1960 | * out of events_attr attributes. | |
1961 | */ | |
1962 | static umode_t | |
1963 | is_visible(struct kobject *kobj, struct attribute *attr, int idx) | |
1964 | { | |
1965 | struct perf_pmu_events_attr *pmu_attr; | |
1966 | ||
1e69a0ef PZ |
1967 | if (idx >= x86_pmu.max_events) |
1968 | return 0; | |
1969 | ||
3d567273 JO |
1970 | pmu_attr = container_of(attr, struct perf_pmu_events_attr, attr.attr); |
1971 | /* str trumps id */ | |
1972 | return pmu_attr->event_str || x86_pmu.event_map(idx) ? attr->mode : 0; | |
1973 | } | |
1974 | ||
2766d2ee | 1975 | static struct attribute_group x86_pmu_events_group __ro_after_init = { |
a4747393 JO |
1976 | .name = "events", |
1977 | .attrs = events_attr, | |
3d567273 | 1978 | .is_visible = is_visible, |
a4747393 JO |
1979 | }; |
1980 | ||
0bf79d44 | 1981 | ssize_t x86_event_sysfs_show(char *page, u64 config, u64 event) |
43c032fe | 1982 | { |
43c032fe JO |
1983 | u64 umask = (config & ARCH_PERFMON_EVENTSEL_UMASK) >> 8; |
1984 | u64 cmask = (config & ARCH_PERFMON_EVENTSEL_CMASK) >> 24; | |
1985 | bool edge = (config & ARCH_PERFMON_EVENTSEL_EDGE); | |
1986 | bool pc = (config & ARCH_PERFMON_EVENTSEL_PIN_CONTROL); | |
1987 | bool any = (config & ARCH_PERFMON_EVENTSEL_ANY); | |
1988 | bool inv = (config & ARCH_PERFMON_EVENTSEL_INV); | |
1989 | ssize_t ret; | |
1990 | ||
1991 | /* | |
1992 | * We have whole page size to spend and just little data | |
1993 | * to write, so we can safely use sprintf. | |
1994 | */ | |
1995 | ret = sprintf(page, "event=0x%02llx", event); | |
1996 | ||
1997 | if (umask) | |
1998 | ret += sprintf(page + ret, ",umask=0x%02llx", umask); | |
1999 | ||
2000 | if (edge) | |
2001 | ret += sprintf(page + ret, ",edge"); | |
2002 | ||
2003 | if (pc) | |
2004 | ret += sprintf(page + ret, ",pc"); | |
2005 | ||
2006 | if (any) | |
2007 | ret += sprintf(page + ret, ",any"); | |
2008 | ||
2009 | if (inv) | |
2010 | ret += sprintf(page + ret, ",inv"); | |
2011 | ||
2012 | if (cmask) | |
2013 | ret += sprintf(page + ret, ",cmask=0x%02llx", cmask); | |
2014 | ||
2015 | ret += sprintf(page + ret, "\n"); | |
2016 | ||
2017 | return ret; | |
2018 | } | |
2019 | ||
6089327f | 2020 | static struct attribute_group x86_pmu_attr_group; |
5da382eb | 2021 | static struct attribute_group x86_pmu_caps_group; |
6089327f | 2022 | |
7c9903c9 PZ |
2023 | static void x86_pmu_static_call_update(void) |
2024 | { | |
2025 | static_call_update(x86_pmu_handle_irq, x86_pmu.handle_irq); | |
2026 | static_call_update(x86_pmu_disable_all, x86_pmu.disable_all); | |
2027 | static_call_update(x86_pmu_enable_all, x86_pmu.enable_all); | |
2028 | static_call_update(x86_pmu_enable, x86_pmu.enable); | |
2029 | static_call_update(x86_pmu_disable, x86_pmu.disable); | |
2030 | ||
8b8ff8cc AH |
2031 | static_call_update(x86_pmu_assign, x86_pmu.assign); |
2032 | ||
7c9903c9 PZ |
2033 | static_call_update(x86_pmu_add, x86_pmu.add); |
2034 | static_call_update(x86_pmu_del, x86_pmu.del); | |
2035 | static_call_update(x86_pmu_read, x86_pmu.read); | |
2036 | ||
73759c34 PZ |
2037 | static_call_update(x86_pmu_set_period, x86_pmu.set_period); |
2038 | static_call_update(x86_pmu_update, x86_pmu.update); | |
08b3068f | 2039 | static_call_update(x86_pmu_limit_period, x86_pmu.limit_period); |
73759c34 | 2040 | |
7c9903c9 PZ |
2041 | static_call_update(x86_pmu_schedule_events, x86_pmu.schedule_events); |
2042 | static_call_update(x86_pmu_get_event_constraints, x86_pmu.get_event_constraints); | |
2043 | static_call_update(x86_pmu_put_event_constraints, x86_pmu.put_event_constraints); | |
2044 | ||
2045 | static_call_update(x86_pmu_start_scheduling, x86_pmu.start_scheduling); | |
2046 | static_call_update(x86_pmu_commit_scheduling, x86_pmu.commit_scheduling); | |
2047 | static_call_update(x86_pmu_stop_scheduling, x86_pmu.stop_scheduling); | |
2048 | ||
2049 | static_call_update(x86_pmu_sched_task, x86_pmu.sched_task); | |
7c9903c9 PZ |
2050 | |
2051 | static_call_update(x86_pmu_drain_pebs, x86_pmu.drain_pebs); | |
2052 | static_call_update(x86_pmu_pebs_aliases, x86_pmu.pebs_aliases); | |
abd562df LX |
2053 | |
2054 | static_call_update(x86_pmu_guest_get_msrs, x86_pmu.guest_get_msrs); | |
bd275681 | 2055 | static_call_update(x86_pmu_filter, x86_pmu.filter); |
e02e9b03 KL |
2056 | |
2057 | static_call_update(x86_pmu_late_setup, x86_pmu.late_setup); | |
4a3fd130 DM |
2058 | |
2059 | static_call_update(x86_pmu_pebs_enable, x86_pmu.pebs_enable); | |
2060 | static_call_update(x86_pmu_pebs_disable, x86_pmu.pebs_disable); | |
2061 | static_call_update(x86_pmu_pebs_enable_all, x86_pmu.pebs_enable_all); | |
2062 | static_call_update(x86_pmu_pebs_disable_all, x86_pmu.pebs_disable_all); | |
7c9903c9 PZ |
2063 | } |
2064 | ||
2065 | static void _x86_pmu_read(struct perf_event *event) | |
2066 | { | |
73759c34 | 2067 | static_call(x86_pmu_update)(event); |
7c9903c9 PZ |
2068 | } |
2069 | ||
722e42e4 | 2070 | void x86_pmu_show_pmu_cap(struct pmu *pmu) |
e11c1a7e KL |
2071 | { |
2072 | pr_info("... version: %d\n", x86_pmu.version); | |
2073 | pr_info("... bit width: %d\n", x86_pmu.cntval_bits); | |
722e42e4 | 2074 | pr_info("... generic registers: %d\n", x86_pmu_num_counters(pmu)); |
e11c1a7e KL |
2075 | pr_info("... value mask: %016Lx\n", x86_pmu.cntval_mask); |
2076 | pr_info("... max period: %016Lx\n", x86_pmu.max_period); | |
722e42e4 KL |
2077 | pr_info("... fixed-purpose events: %d\n", x86_pmu_num_counters_fixed(pmu)); |
2078 | pr_info("... event mask: %016Lx\n", hybrid(pmu, intel_ctrl)); | |
e11c1a7e KL |
2079 | } |
2080 | ||
dda99116 | 2081 | static int __init init_hw_perf_events(void) |
b56a3802 | 2082 | { |
c1d6f42f | 2083 | struct x86_pmu_quirk *quirk; |
72eae04d RR |
2084 | int err; |
2085 | ||
cdd6c482 | 2086 | pr_info("Performance Events: "); |
1123e3ad | 2087 | |
b56a3802 JSR |
2088 | switch (boot_cpu_data.x86_vendor) { |
2089 | case X86_VENDOR_INTEL: | |
72eae04d | 2090 | err = intel_pmu_init(); |
b56a3802 | 2091 | break; |
f87ad35d | 2092 | case X86_VENDOR_AMD: |
72eae04d | 2093 | err = amd_pmu_init(); |
f87ad35d | 2094 | break; |
6d0ef316 PW |
2095 | case X86_VENDOR_HYGON: |
2096 | err = amd_pmu_init(); | |
2097 | x86_pmu.name = "HYGON"; | |
2098 | break; | |
3a4ac121 C |
2099 | case X86_VENDOR_ZHAOXIN: |
2100 | case X86_VENDOR_CENTAUR: | |
2101 | err = zhaoxin_pmu_init(); | |
2102 | break; | |
4138960a | 2103 | default: |
8a3da6c7 | 2104 | err = -ENOTSUPP; |
b56a3802 | 2105 | } |
1123e3ad | 2106 | if (err != 0) { |
cdd6c482 | 2107 | pr_cont("no PMU driver, software events only.\n"); |
916e3a4f PB |
2108 | err = 0; |
2109 | goto out_bad_pmu; | |
1123e3ad | 2110 | } |
b56a3802 | 2111 | |
12558038 CG |
2112 | pmu_check_apic(); |
2113 | ||
33c6d6a7 | 2114 | /* sanity check that the hardware exists or is emulated */ |
722e42e4 | 2115 | if (!check_hw_exists(&pmu, x86_pmu.cntr_mask, x86_pmu.fixed_cntr_mask)) |
916e3a4f | 2116 | goto out_bad_pmu; |
33c6d6a7 | 2117 | |
1123e3ad | 2118 | pr_cont("%s PMU driver.\n", x86_pmu.name); |
faa28ae0 | 2119 | |
e97df763 PZ |
2120 | x86_pmu.attr_rdpmc = 1; /* enable userspace RDPMC usage by default */ |
2121 | ||
c1d6f42f PZ |
2122 | for (quirk = x86_pmu.quirks; quirk; quirk = quirk->next) |
2123 | quirk->func(); | |
3c44780b | 2124 | |
a1eac7ac | 2125 | if (!x86_pmu.intel_ctrl) |
722e42e4 | 2126 | x86_pmu.intel_ctrl = x86_pmu.cntr_mask64; |
241771ef | 2127 | |
e8fb5d6e KL |
2128 | if (!x86_pmu.config_mask) |
2129 | x86_pmu.config_mask = X86_RAW_EVENT_MASK; | |
241771ef | 2130 | |
cdd6c482 | 2131 | perf_events_lapic_init(); |
9c48f1c6 | 2132 | register_nmi_handler(NMI_LOCAL, perf_event_nmi_handler, 0, "PMI"); |
1123e3ad | 2133 | |
63b14649 | 2134 | unconstrained = (struct event_constraint) |
722e42e4 KL |
2135 | __EVENT_CONSTRAINT(0, x86_pmu.cntr_mask64, |
2136 | 0, x86_pmu_num_counters(NULL), 0, 0); | |
63b14649 | 2137 | |
641cc938 | 2138 | x86_pmu_format_group.attrs = x86_pmu.format_attrs; |
0c9d42ed | 2139 | |
a4747393 JO |
2140 | if (!x86_pmu.events_sysfs_show) |
2141 | x86_pmu_events_group.attrs = &empty_attrs; | |
1a6461b1 | 2142 | |
baa0c833 | 2143 | pmu.attr_update = x86_pmu.attr_update; |
6089327f | 2144 | |
722e42e4 KL |
2145 | if (!is_hybrid()) |
2146 | x86_pmu_show_pmu_cap(NULL); | |
3f6da390 | 2147 | |
7c9903c9 PZ |
2148 | if (!x86_pmu.read) |
2149 | x86_pmu.read = _x86_pmu_read; | |
2150 | ||
abd562df | 2151 | if (!x86_pmu.guest_get_msrs) |
c8e2fe13 | 2152 | x86_pmu.guest_get_msrs = (void *)&__static_call_return0; |
abd562df | 2153 | |
73759c34 PZ |
2154 | if (!x86_pmu.set_period) |
2155 | x86_pmu.set_period = x86_perf_event_set_period; | |
2156 | ||
2157 | if (!x86_pmu.update) | |
2158 | x86_pmu.update = x86_perf_event_update; | |
2159 | ||
7c9903c9 PZ |
2160 | x86_pmu_static_call_update(); |
2161 | ||
95ca792c TG |
2162 | /* |
2163 | * Install callbacks. Core will call them for each online | |
2164 | * cpu. | |
2165 | */ | |
73c1b41e | 2166 | err = cpuhp_setup_state(CPUHP_PERF_X86_PREPARE, "perf/x86:prepare", |
95ca792c TG |
2167 | x86_pmu_prepare_cpu, x86_pmu_dead_cpu); |
2168 | if (err) | |
2169 | return err; | |
2170 | ||
2171 | err = cpuhp_setup_state(CPUHP_AP_PERF_X86_STARTING, | |
73c1b41e | 2172 | "perf/x86:starting", x86_pmu_starting_cpu, |
95ca792c TG |
2173 | x86_pmu_dying_cpu); |
2174 | if (err) | |
2175 | goto out; | |
2176 | ||
73c1b41e | 2177 | err = cpuhp_setup_state(CPUHP_AP_PERF_X86_ONLINE, "perf/x86:online", |
95ca792c TG |
2178 | x86_pmu_online_cpu, NULL); |
2179 | if (err) | |
2180 | goto out1; | |
2181 | ||
d9977c43 KL |
2182 | if (!is_hybrid()) { |
2183 | err = perf_pmu_register(&pmu, "cpu", PERF_TYPE_RAW); | |
2184 | if (err) | |
2185 | goto out2; | |
2186 | } else { | |
d9977c43 KL |
2187 | struct x86_hybrid_pmu *hybrid_pmu; |
2188 | int i, j; | |
2189 | ||
d9977c43 KL |
2190 | for (i = 0; i < x86_pmu.num_hybrid_pmus; i++) { |
2191 | hybrid_pmu = &x86_pmu.hybrid_pmu[i]; | |
2192 | ||
2193 | hybrid_pmu->pmu = pmu; | |
2194 | hybrid_pmu->pmu.type = -1; | |
2195 | hybrid_pmu->pmu.attr_update = x86_pmu.attr_update; | |
55bcf6ef | 2196 | hybrid_pmu->pmu.capabilities |= PERF_PMU_CAP_EXTENDED_HW_TYPE; |
d9977c43 KL |
2197 | |
2198 | err = perf_pmu_register(&hybrid_pmu->pmu, hybrid_pmu->name, | |
b0560bfd | 2199 | (hybrid_pmu->pmu_type == hybrid_big) ? PERF_TYPE_RAW : -1); |
d9977c43 KL |
2200 | if (err) |
2201 | break; | |
d9977c43 KL |
2202 | } |
2203 | ||
2204 | if (i < x86_pmu.num_hybrid_pmus) { | |
2205 | for (j = 0; j < i; j++) | |
2206 | perf_pmu_unregister(&x86_pmu.hybrid_pmu[j].pmu); | |
2207 | pr_warn("Failed to register hybrid PMUs\n"); | |
2208 | kfree(x86_pmu.hybrid_pmu); | |
2209 | x86_pmu.hybrid_pmu = NULL; | |
2210 | x86_pmu.num_hybrid_pmus = 0; | |
2211 | goto out2; | |
2212 | } | |
2213 | } | |
004417a6 PZ |
2214 | |
2215 | return 0; | |
95ca792c TG |
2216 | |
2217 | out2: | |
2218 | cpuhp_remove_state(CPUHP_AP_PERF_X86_ONLINE); | |
2219 | out1: | |
2220 | cpuhp_remove_state(CPUHP_AP_PERF_X86_STARTING); | |
2221 | out: | |
2222 | cpuhp_remove_state(CPUHP_PERF_X86_PREPARE); | |
916e3a4f PB |
2223 | out_bad_pmu: |
2224 | memset(&x86_pmu, 0, sizeof(x86_pmu)); | |
95ca792c | 2225 | return err; |
241771ef | 2226 | } |
004417a6 | 2227 | early_initcall(init_hw_perf_events); |
621a01ea | 2228 | |
7c9903c9 | 2229 | static void x86_pmu_read(struct perf_event *event) |
ee06094f | 2230 | { |
7c9903c9 | 2231 | static_call(x86_pmu_read)(event); |
ee06094f IM |
2232 | } |
2233 | ||
4d1c52b0 LM |
2234 | /* |
2235 | * Start group events scheduling transaction | |
2236 | * Set the flag to make pmu::enable() not perform the | |
2237 | * schedulability test, it will be performed at commit time | |
fbbe0701 SB |
2238 | * |
2239 | * We only support PERF_PMU_TXN_ADD transactions. Save the | |
2240 | * transaction flags but otherwise ignore non-PERF_PMU_TXN_ADD | |
2241 | * transactions. | |
4d1c52b0 | 2242 | */ |
fbbe0701 | 2243 | static void x86_pmu_start_txn(struct pmu *pmu, unsigned int txn_flags) |
4d1c52b0 | 2244 | { |
fbbe0701 SB |
2245 | struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); |
2246 | ||
2247 | WARN_ON_ONCE(cpuc->txn_flags); /* txn already in flight */ | |
2248 | ||
2249 | cpuc->txn_flags = txn_flags; | |
2250 | if (txn_flags & ~PERF_PMU_TXN_ADD) | |
2251 | return; | |
2252 | ||
33696fc0 | 2253 | perf_pmu_disable(pmu); |
0a3aee0d | 2254 | __this_cpu_write(cpu_hw_events.n_txn, 0); |
871a93b0 | 2255 | __this_cpu_write(cpu_hw_events.n_txn_pair, 0); |
3dbde695 | 2256 | __this_cpu_write(cpu_hw_events.n_txn_metric, 0); |
4d1c52b0 LM |
2257 | } |
2258 | ||
2259 | /* | |
2260 | * Stop group events scheduling transaction | |
2261 | * Clear the flag and pmu::enable() will perform the | |
2262 | * schedulability test. | |
2263 | */ | |
51b0fe39 | 2264 | static void x86_pmu_cancel_txn(struct pmu *pmu) |
4d1c52b0 | 2265 | { |
fbbe0701 SB |
2266 | unsigned int txn_flags; |
2267 | struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); | |
2268 | ||
2269 | WARN_ON_ONCE(!cpuc->txn_flags); /* no txn in flight */ | |
2270 | ||
2271 | txn_flags = cpuc->txn_flags; | |
2272 | cpuc->txn_flags = 0; | |
2273 | if (txn_flags & ~PERF_PMU_TXN_ADD) | |
2274 | return; | |
2275 | ||
90151c35 | 2276 | /* |
c347a2f1 PZ |
2277 | * Truncate collected array by the number of events added in this |
2278 | * transaction. See x86_pmu_add() and x86_pmu_*_txn(). | |
90151c35 | 2279 | */ |
0a3aee0d TH |
2280 | __this_cpu_sub(cpu_hw_events.n_added, __this_cpu_read(cpu_hw_events.n_txn)); |
2281 | __this_cpu_sub(cpu_hw_events.n_events, __this_cpu_read(cpu_hw_events.n_txn)); | |
871a93b0 | 2282 | __this_cpu_sub(cpu_hw_events.n_pair, __this_cpu_read(cpu_hw_events.n_txn_pair)); |
3dbde695 | 2283 | __this_cpu_sub(cpu_hw_events.n_metric, __this_cpu_read(cpu_hw_events.n_txn_metric)); |
33696fc0 | 2284 | perf_pmu_enable(pmu); |
4d1c52b0 LM |
2285 | } |
2286 | ||
2287 | /* | |
2288 | * Commit group events scheduling transaction | |
2289 | * Perform the group schedulability test as a whole | |
2290 | * Return 0 if success | |
c347a2f1 PZ |
2291 | * |
2292 | * Does not cancel the transaction on failure; expects the caller to do this. | |
4d1c52b0 | 2293 | */ |
51b0fe39 | 2294 | static int x86_pmu_commit_txn(struct pmu *pmu) |
4d1c52b0 | 2295 | { |
89cbc767 | 2296 | struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); |
4d1c52b0 LM |
2297 | int assign[X86_PMC_IDX_MAX]; |
2298 | int n, ret; | |
2299 | ||
fbbe0701 SB |
2300 | WARN_ON_ONCE(!cpuc->txn_flags); /* no txn in flight */ |
2301 | ||
2302 | if (cpuc->txn_flags & ~PERF_PMU_TXN_ADD) { | |
2303 | cpuc->txn_flags = 0; | |
2304 | return 0; | |
2305 | } | |
2306 | ||
4d1c52b0 LM |
2307 | n = cpuc->n_events; |
2308 | ||
2309 | if (!x86_pmu_initialized()) | |
2310 | return -EAGAIN; | |
2311 | ||
7c9903c9 | 2312 | ret = static_call(x86_pmu_schedule_events)(cpuc, n, assign); |
4d1c52b0 LM |
2313 | if (ret) |
2314 | return ret; | |
2315 | ||
2316 | /* | |
2317 | * copy new assignment, now we know it is possible | |
2318 | * will be used by hw_perf_enable() | |
2319 | */ | |
2320 | memcpy(cpuc->assign, assign, n*sizeof(int)); | |
2321 | ||
fbbe0701 | 2322 | cpuc->txn_flags = 0; |
33696fc0 | 2323 | perf_pmu_enable(pmu); |
4d1c52b0 LM |
2324 | return 0; |
2325 | } | |
cd8a38d3 SE |
2326 | /* |
2327 | * a fake_cpuc is used to validate event groups. Due to | |
2328 | * the extra reg logic, we need to also allocate a fake | |
2329 | * per_core and per_cpu structure. Otherwise, group events | |
2330 | * using extra reg may conflict without the kernel being | |
2331 | * able to catch this when the last event gets added to | |
2332 | * the group. | |
2333 | */ | |
2334 | static void free_fake_cpuc(struct cpu_hw_events *cpuc) | |
2335 | { | |
d01b1f96 | 2336 | intel_cpuc_finish(cpuc); |
cd8a38d3 SE |
2337 | kfree(cpuc); |
2338 | } | |
2339 | ||
d9977c43 | 2340 | static struct cpu_hw_events *allocate_fake_cpuc(struct pmu *event_pmu) |
cd8a38d3 SE |
2341 | { |
2342 | struct cpu_hw_events *cpuc; | |
d9977c43 | 2343 | int cpu; |
cd8a38d3 SE |
2344 | |
2345 | cpuc = kzalloc(sizeof(*cpuc), GFP_KERNEL); | |
2346 | if (!cpuc) | |
2347 | return ERR_PTR(-ENOMEM); | |
b430f7c4 | 2348 | cpuc->is_fake = 1; |
d01b1f96 | 2349 | |
d9977c43 KL |
2350 | if (is_hybrid()) { |
2351 | struct x86_hybrid_pmu *h_pmu; | |
2352 | ||
2353 | h_pmu = hybrid_pmu(event_pmu); | |
2354 | if (cpumask_empty(&h_pmu->supported_cpus)) | |
2355 | goto error; | |
2356 | cpu = cpumask_first(&h_pmu->supported_cpus); | |
2357 | } else | |
2358 | cpu = raw_smp_processor_id(); | |
2359 | cpuc->pmu = event_pmu; | |
2360 | ||
d01b1f96 PZI |
2361 | if (intel_cpuc_prepare(cpuc, cpu)) |
2362 | goto error; | |
2363 | ||
cd8a38d3 SE |
2364 | return cpuc; |
2365 | error: | |
2366 | free_fake_cpuc(cpuc); | |
2367 | return ERR_PTR(-ENOMEM); | |
2368 | } | |
4d1c52b0 | 2369 | |
ca037701 PZ |
2370 | /* |
2371 | * validate that we can schedule this event | |
2372 | */ | |
2373 | static int validate_event(struct perf_event *event) | |
2374 | { | |
2375 | struct cpu_hw_events *fake_cpuc; | |
2376 | struct event_constraint *c; | |
2377 | int ret = 0; | |
2378 | ||
d9977c43 | 2379 | fake_cpuc = allocate_fake_cpuc(event->pmu); |
cd8a38d3 SE |
2380 | if (IS_ERR(fake_cpuc)) |
2381 | return PTR_ERR(fake_cpuc); | |
ca037701 | 2382 | |
21d65555 | 2383 | c = x86_pmu.get_event_constraints(fake_cpuc, 0, event); |
ca037701 PZ |
2384 | |
2385 | if (!c || !c->weight) | |
aa2bc1ad | 2386 | ret = -EINVAL; |
ca037701 PZ |
2387 | |
2388 | if (x86_pmu.put_event_constraints) | |
2389 | x86_pmu.put_event_constraints(fake_cpuc, event); | |
2390 | ||
cd8a38d3 | 2391 | free_fake_cpuc(fake_cpuc); |
ca037701 PZ |
2392 | |
2393 | return ret; | |
2394 | } | |
2395 | ||
1da53e02 SE |
2396 | /* |
2397 | * validate a single event group | |
2398 | * | |
2399 | * validation include: | |
184f412c IM |
2400 | * - check events are compatible which each other |
2401 | * - events do not compete for the same counter | |
2402 | * - number of events <= number of counters | |
1da53e02 SE |
2403 | * |
2404 | * validation ensures the group can be loaded onto the | |
2405 | * PMU if it was the only group available. | |
2406 | */ | |
fe9081cc PZ |
2407 | static int validate_group(struct perf_event *event) |
2408 | { | |
1da53e02 | 2409 | struct perf_event *leader = event->group_leader; |
502568d5 | 2410 | struct cpu_hw_events *fake_cpuc; |
aa2bc1ad | 2411 | int ret = -EINVAL, n; |
fe9081cc | 2412 | |
d9977c43 KL |
2413 | /* |
2414 | * Reject events from different hybrid PMUs. | |
2415 | */ | |
2416 | if (is_hybrid()) { | |
2417 | struct perf_event *sibling; | |
2418 | struct pmu *pmu = NULL; | |
2419 | ||
2420 | if (is_x86_event(leader)) | |
2421 | pmu = leader->pmu; | |
2422 | ||
2423 | for_each_sibling_event(sibling, leader) { | |
2424 | if (!is_x86_event(sibling)) | |
2425 | continue; | |
2426 | if (!pmu) | |
2427 | pmu = sibling->pmu; | |
2428 | else if (pmu != sibling->pmu) | |
2429 | return ret; | |
2430 | } | |
2431 | } | |
2432 | ||
2433 | fake_cpuc = allocate_fake_cpuc(event->pmu); | |
cd8a38d3 SE |
2434 | if (IS_ERR(fake_cpuc)) |
2435 | return PTR_ERR(fake_cpuc); | |
1da53e02 SE |
2436 | /* |
2437 | * the event is not yet connected with its | |
2438 | * siblings therefore we must first collect | |
2439 | * existing siblings, then add the new event | |
2440 | * before we can simulate the scheduling | |
2441 | */ | |
502568d5 | 2442 | n = collect_events(fake_cpuc, leader, true); |
1da53e02 | 2443 | if (n < 0) |
cd8a38d3 | 2444 | goto out; |
fe9081cc | 2445 | |
502568d5 PZ |
2446 | fake_cpuc->n_events = n; |
2447 | n = collect_events(fake_cpuc, event, false); | |
1da53e02 | 2448 | if (n < 0) |
cd8a38d3 | 2449 | goto out; |
fe9081cc | 2450 | |
1f6a1e2d | 2451 | fake_cpuc->n_events = 0; |
a072738e | 2452 | ret = x86_pmu.schedule_events(fake_cpuc, n, NULL); |
502568d5 | 2453 | |
502568d5 | 2454 | out: |
cd8a38d3 | 2455 | free_fake_cpuc(fake_cpuc); |
502568d5 | 2456 | return ret; |
fe9081cc PZ |
2457 | } |
2458 | ||
dda99116 | 2459 | static int x86_pmu_event_init(struct perf_event *event) |
621a01ea | 2460 | { |
d9977c43 | 2461 | struct x86_hybrid_pmu *pmu = NULL; |
621a01ea IM |
2462 | int err; |
2463 | ||
d9977c43 KL |
2464 | if ((event->attr.type != event->pmu->type) && |
2465 | (event->attr.type != PERF_TYPE_HARDWARE) && | |
2466 | (event->attr.type != PERF_TYPE_HW_CACHE)) | |
b0a873eb | 2467 | return -ENOENT; |
d9977c43 KL |
2468 | |
2469 | if (is_hybrid() && (event->cpu != -1)) { | |
2470 | pmu = hybrid_pmu(event->pmu); | |
2471 | if (!cpumask_test_cpu(event->cpu, &pmu->supported_cpus)) | |
2472 | return -ENOENT; | |
b0a873eb PZ |
2473 | } |
2474 | ||
2475 | err = __x86_pmu_event_init(event); | |
fe9081cc PZ |
2476 | if (!err) { |
2477 | if (event->group_leader != event) | |
2478 | err = validate_group(event); | |
ca037701 PZ |
2479 | else |
2480 | err = validate_event(event); | |
fe9081cc | 2481 | } |
a1792cda | 2482 | if (err) { |
cdd6c482 IM |
2483 | if (event->destroy) |
2484 | event->destroy(event); | |
02d029a4 | 2485 | event->destroy = NULL; |
a1792cda | 2486 | } |
621a01ea | 2487 | |
1af22eba | 2488 | if (READ_ONCE(x86_pmu.attr_rdpmc) && |
174afc3e | 2489 | !(event->hw.flags & PERF_X86_EVENT_LARGE_PEBS)) |
369461ce | 2490 | event->hw.flags |= PERF_EVENT_FLAG_USER_READ_CNT; |
7911d3f7 | 2491 | |
b0a873eb | 2492 | return err; |
621a01ea | 2493 | } |
d7d59fb3 | 2494 | |
5471eea5 KL |
2495 | void perf_clear_dirty_counters(void) |
2496 | { | |
2497 | struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); | |
2498 | int i; | |
2499 | ||
2500 | /* Don't need to clear the assigned counter. */ | |
2501 | for (i = 0; i < cpuc->n_events; i++) | |
2502 | __clear_bit(cpuc->assign[i], cpuc->dirty); | |
2503 | ||
2504 | if (bitmap_empty(cpuc->dirty, X86_PMC_IDX_MAX)) | |
2505 | return; | |
2506 | ||
2507 | for_each_set_bit(i, cpuc->dirty, X86_PMC_IDX_MAX) { | |
f4b4b456 PZ |
2508 | if (i >= INTEL_PMC_IDX_FIXED) { |
2509 | /* Metrics and fake events don't have corresponding HW counters. */ | |
722e42e4 | 2510 | if (!test_bit(i - INTEL_PMC_IDX_FIXED, hybrid(cpuc->pmu, fixed_cntr_mask))) |
f4b4b456 PZ |
2511 | continue; |
2512 | ||
78255eb2 | 2513 | wrmsrq(x86_pmu_fixed_ctr_addr(i - INTEL_PMC_IDX_FIXED), 0); |
f4b4b456 | 2514 | } else { |
78255eb2 | 2515 | wrmsrq(x86_pmu_event_addr(i), 0); |
f4b4b456 | 2516 | } |
5471eea5 KL |
2517 | } |
2518 | ||
2519 | bitmap_zero(cpuc->dirty, X86_PMC_IDX_MAX); | |
2520 | } | |
2521 | ||
bfe33492 | 2522 | static void x86_pmu_event_mapped(struct perf_event *event, struct mm_struct *mm) |
7911d3f7 | 2523 | { |
369461ce | 2524 | if (!(event->hw.flags & PERF_EVENT_FLAG_USER_READ_CNT)) |
7911d3f7 AL |
2525 | return; |
2526 | ||
4b07372a AL |
2527 | /* |
2528 | * This function relies on not being called concurrently in two | |
2529 | * tasks in the same mm. Otherwise one task could observe | |
2530 | * perf_rdpmc_allowed > 1 and return all the way back to | |
2531 | * userspace with CR4.PCE clear while another task is still | |
2532 | * doing on_each_cpu_mask() to propagate CR4.PCE. | |
2533 | * | |
c1e8d7c6 | 2534 | * For now, this can't happen because all callers hold mmap_lock |
4b07372a AL |
2535 | * for write. If this changes, we'll need a different solution. |
2536 | */ | |
42fc5414 | 2537 | mmap_assert_write_locked(mm); |
4b07372a | 2538 | |
bfe33492 | 2539 | if (atomic_inc_return(&mm->context.perf_rdpmc_allowed) == 1) |
cb2a0235 | 2540 | on_each_cpu_mask(mm_cpumask(mm), cr4_update_pce, NULL, 1); |
7911d3f7 AL |
2541 | } |
2542 | ||
bfe33492 | 2543 | static void x86_pmu_event_unmapped(struct perf_event *event, struct mm_struct *mm) |
7911d3f7 | 2544 | { |
369461ce | 2545 | if (!(event->hw.flags & PERF_EVENT_FLAG_USER_READ_CNT)) |
7911d3f7 AL |
2546 | return; |
2547 | ||
bfe33492 | 2548 | if (atomic_dec_and_test(&mm->context.perf_rdpmc_allowed)) |
cb2a0235 | 2549 | on_each_cpu_mask(mm_cpumask(mm), cr4_update_pce, NULL, 1); |
7911d3f7 AL |
2550 | } |
2551 | ||
fe4a3308 PZ |
2552 | static int x86_pmu_event_idx(struct perf_event *event) |
2553 | { | |
75608cb0 | 2554 | struct hw_perf_event *hwc = &event->hw; |
fe4a3308 | 2555 | |
369461ce | 2556 | if (!(hwc->flags & PERF_EVENT_FLAG_USER_READ_CNT)) |
c7206205 PZ |
2557 | return 0; |
2558 | ||
2cb5383b KL |
2559 | if (is_metric_idx(hwc->idx)) |
2560 | return INTEL_PMC_FIXED_RDPMC_METRICS + 1; | |
2561 | else | |
2562 | return hwc->event_base_rdpmc + 1; | |
fe4a3308 PZ |
2563 | } |
2564 | ||
0c9d42ed PZ |
2565 | static ssize_t get_attr_rdpmc(struct device *cdev, |
2566 | struct device_attribute *attr, | |
2567 | char *buf) | |
2568 | { | |
2569 | return snprintf(buf, 40, "%d\n", x86_pmu.attr_rdpmc); | |
2570 | } | |
2571 | ||
0c9d42ed PZ |
2572 | static ssize_t set_attr_rdpmc(struct device *cdev, |
2573 | struct device_attribute *attr, | |
2574 | const char *buf, size_t count) | |
2575 | { | |
bb9bb45f | 2576 | static DEFINE_MUTEX(rdpmc_mutex); |
e2b297fc SK |
2577 | unsigned long val; |
2578 | ssize_t ret; | |
2579 | ||
2580 | ret = kstrtoul(buf, 0, &val); | |
2581 | if (ret) | |
2582 | return ret; | |
e97df763 | 2583 | |
a6673429 AL |
2584 | if (val > 2) |
2585 | return -EINVAL; | |
2586 | ||
e97df763 PZ |
2587 | if (x86_pmu.attr_rdpmc_broken) |
2588 | return -ENOTSUPP; | |
0c9d42ed | 2589 | |
bb9bb45f TG |
2590 | guard(mutex)(&rdpmc_mutex); |
2591 | ||
405b4537 | 2592 | if (val != x86_pmu.attr_rdpmc) { |
a6673429 | 2593 | /* |
405b4537 AS |
2594 | * Changing into or out of never available or always available, |
2595 | * aka perf-event-bypassing mode. This path is extremely slow, | |
a6673429 AL |
2596 | * but only root can trigger it, so it's okay. |
2597 | */ | |
405b4537 AS |
2598 | if (val == 0) |
2599 | static_branch_inc(&rdpmc_never_available_key); | |
2600 | else if (x86_pmu.attr_rdpmc == 0) | |
2601 | static_branch_dec(&rdpmc_never_available_key); | |
2602 | ||
a6673429 | 2603 | if (val == 2) |
631fe154 | 2604 | static_branch_inc(&rdpmc_always_available_key); |
405b4537 | 2605 | else if (x86_pmu.attr_rdpmc == 2) |
631fe154 | 2606 | static_branch_dec(&rdpmc_always_available_key); |
405b4537 | 2607 | |
cb2a0235 | 2608 | on_each_cpu(cr4_update_pce, NULL, 1); |
405b4537 | 2609 | x86_pmu.attr_rdpmc = val; |
a6673429 AL |
2610 | } |
2611 | ||
0c9d42ed PZ |
2612 | return count; |
2613 | } | |
2614 | ||
2615 | static DEVICE_ATTR(rdpmc, S_IRUSR | S_IWUSR, get_attr_rdpmc, set_attr_rdpmc); | |
2616 | ||
2617 | static struct attribute *x86_pmu_attrs[] = { | |
2618 | &dev_attr_rdpmc.attr, | |
2619 | NULL, | |
2620 | }; | |
2621 | ||
2766d2ee | 2622 | static struct attribute_group x86_pmu_attr_group __ro_after_init = { |
0c9d42ed PZ |
2623 | .attrs = x86_pmu_attrs, |
2624 | }; | |
2625 | ||
5da382eb PZ |
2626 | static ssize_t max_precise_show(struct device *cdev, |
2627 | struct device_attribute *attr, | |
2628 | char *buf) | |
2629 | { | |
2630 | return snprintf(buf, PAGE_SIZE, "%d\n", x86_pmu_max_precise()); | |
2631 | } | |
2632 | ||
2633 | static DEVICE_ATTR_RO(max_precise); | |
2634 | ||
2635 | static struct attribute *x86_pmu_caps_attrs[] = { | |
2636 | &dev_attr_max_precise.attr, | |
2637 | NULL | |
2638 | }; | |
2639 | ||
2766d2ee | 2640 | static struct attribute_group x86_pmu_caps_group __ro_after_init = { |
5da382eb PZ |
2641 | .name = "caps", |
2642 | .attrs = x86_pmu_caps_attrs, | |
2643 | }; | |
2644 | ||
0c9d42ed PZ |
2645 | static const struct attribute_group *x86_pmu_attr_groups[] = { |
2646 | &x86_pmu_attr_group, | |
641cc938 | 2647 | &x86_pmu_format_group, |
a4747393 | 2648 | &x86_pmu_events_group, |
b00233b5 | 2649 | &x86_pmu_caps_group, |
0c9d42ed PZ |
2650 | NULL, |
2651 | }; | |
2652 | ||
d57e94f5 KL |
2653 | static void x86_pmu_sched_task(struct perf_event_pmu_context *pmu_ctx, |
2654 | struct task_struct *task, bool sched_in) | |
d010b332 | 2655 | { |
d57e94f5 | 2656 | static_call_cond(x86_pmu_sched_task)(pmu_ctx, task, sched_in); |
d010b332 SE |
2657 | } |
2658 | ||
c93dc84c PZ |
2659 | void perf_check_microcode(void) |
2660 | { | |
2661 | if (x86_pmu.check_microcode) | |
2662 | x86_pmu.check_microcode(); | |
2663 | } | |
c93dc84c | 2664 | |
81ec3f3c JO |
2665 | static int x86_pmu_check_period(struct perf_event *event, u64 value) |
2666 | { | |
2667 | if (x86_pmu.check_period && x86_pmu.check_period(event, value)) | |
2668 | return -EINVAL; | |
2669 | ||
2670 | if (value && x86_pmu.limit_period) { | |
28f0f3c4 PZ |
2671 | s64 left = value; |
2672 | x86_pmu.limit_period(event, &left); | |
2673 | if (left > value) | |
81ec3f3c JO |
2674 | return -EINVAL; |
2675 | } | |
2676 | ||
2677 | return 0; | |
2678 | } | |
2679 | ||
42880f72 AS |
2680 | static int x86_pmu_aux_output_match(struct perf_event *event) |
2681 | { | |
2682 | if (!(pmu.capabilities & PERF_PMU_CAP_AUX_OUTPUT)) | |
2683 | return 0; | |
2684 | ||
2685 | if (x86_pmu.aux_output_match) | |
2686 | return x86_pmu.aux_output_match(event); | |
2687 | ||
2688 | return 0; | |
2689 | } | |
2690 | ||
bd275681 | 2691 | static bool x86_pmu_filter(struct pmu *pmu, int cpu) |
3e9a8b21 | 2692 | { |
bd275681 PZ |
2693 | bool ret = false; |
2694 | ||
2695 | static_call_cond(x86_pmu_filter)(pmu, cpu, &ret); | |
3e9a8b21 | 2696 | |
bd275681 | 2697 | return ret; |
3e9a8b21 KL |
2698 | } |
2699 | ||
b0a873eb | 2700 | static struct pmu pmu = { |
d010b332 SE |
2701 | .pmu_enable = x86_pmu_enable, |
2702 | .pmu_disable = x86_pmu_disable, | |
a4eaf7f1 | 2703 | |
c93dc84c | 2704 | .attr_groups = x86_pmu_attr_groups, |
0c9d42ed | 2705 | |
c93dc84c | 2706 | .event_init = x86_pmu_event_init, |
a4eaf7f1 | 2707 | |
7911d3f7 AL |
2708 | .event_mapped = x86_pmu_event_mapped, |
2709 | .event_unmapped = x86_pmu_event_unmapped, | |
2710 | ||
d010b332 SE |
2711 | .add = x86_pmu_add, |
2712 | .del = x86_pmu_del, | |
2713 | .start = x86_pmu_start, | |
2714 | .stop = x86_pmu_stop, | |
2715 | .read = x86_pmu_read, | |
a4eaf7f1 | 2716 | |
c93dc84c PZ |
2717 | .start_txn = x86_pmu_start_txn, |
2718 | .cancel_txn = x86_pmu_cancel_txn, | |
2719 | .commit_txn = x86_pmu_commit_txn, | |
fe4a3308 | 2720 | |
c93dc84c | 2721 | .event_idx = x86_pmu_event_idx, |
ba532500 | 2722 | .sched_task = x86_pmu_sched_task, |
81ec3f3c | 2723 | .check_period = x86_pmu_check_period, |
42880f72 AS |
2724 | |
2725 | .aux_output_match = x86_pmu_aux_output_match, | |
3e9a8b21 | 2726 | |
bd275681 | 2727 | .filter = x86_pmu_filter, |
b0a873eb PZ |
2728 | }; |
2729 | ||
c1317ec2 AL |
2730 | void arch_perf_update_userpage(struct perf_event *event, |
2731 | struct perf_event_mmap_page *userpg, u64 now) | |
e3f3541c | 2732 | { |
59eaef78 | 2733 | struct cyc2ns_data data; |
698eff63 | 2734 | u64 offset; |
20d1c86a | 2735 | |
fa731587 PZ |
2736 | userpg->cap_user_time = 0; |
2737 | userpg->cap_user_time_zero = 0; | |
7911d3f7 | 2738 | userpg->cap_user_rdpmc = |
369461ce | 2739 | !!(event->hw.flags & PERF_EVENT_FLAG_USER_READ_CNT); |
c7206205 PZ |
2740 | userpg->pmc_width = x86_pmu.cntval_bits; |
2741 | ||
698eff63 | 2742 | if (!using_native_sched_clock() || !sched_clock_stable()) |
e3f3541c PZ |
2743 | return; |
2744 | ||
59eaef78 | 2745 | cyc2ns_read_begin(&data); |
20d1c86a | 2746 | |
59eaef78 | 2747 | offset = data.cyc2ns_offset + __sched_clock_offset; |
698eff63 | 2748 | |
34f43927 PZ |
2749 | /* |
2750 | * Internal timekeeping for enabled/running/stopped times | |
2751 | * is always in the local_clock domain. | |
2752 | */ | |
fa731587 | 2753 | userpg->cap_user_time = 1; |
59eaef78 PZ |
2754 | userpg->time_mult = data.cyc2ns_mul; |
2755 | userpg->time_shift = data.cyc2ns_shift; | |
698eff63 | 2756 | userpg->time_offset = offset - now; |
c73deb6a | 2757 | |
34f43927 PZ |
2758 | /* |
2759 | * cap_user_time_zero doesn't make sense when we're using a different | |
2760 | * time base for the records. | |
2761 | */ | |
f454bfdd | 2762 | if (!event->attr.use_clockid) { |
34f43927 | 2763 | userpg->cap_user_time_zero = 1; |
698eff63 | 2764 | userpg->time_zero = offset; |
34f43927 | 2765 | } |
20d1c86a | 2766 | |
59eaef78 | 2767 | cyc2ns_read_end(); |
e3f3541c PZ |
2768 | } |
2769 | ||
d15d3568 KS |
2770 | /* |
2771 | * Determine whether the regs were taken from an irq/exception handler rather | |
2772 | * than from perf_arch_fetch_caller_regs(). | |
2773 | */ | |
2774 | static bool perf_hw_regs(struct pt_regs *regs) | |
2775 | { | |
2776 | return regs->flags & X86_EFLAGS_FIXED; | |
2777 | } | |
2778 | ||
56962b44 | 2779 | void |
cfbcf468 | 2780 | perf_callchain_kernel(struct perf_callchain_entry_ctx *entry, struct pt_regs *regs) |
d7d59fb3 | 2781 | { |
35f4d9b3 JP |
2782 | struct unwind_state state; |
2783 | unsigned long addr; | |
2784 | ||
1c343051 | 2785 | if (perf_guest_state()) { |
927c7a9e | 2786 | /* TODO: We don't support guest os callchain now */ |
ed805261 | 2787 | return; |
927c7a9e FW |
2788 | } |
2789 | ||
83f44ae0 SL |
2790 | if (perf_callchain_store(entry, regs->ip)) |
2791 | return; | |
2792 | ||
2793 | if (perf_hw_regs(regs)) | |
d15d3568 | 2794 | unwind_start(&state, current, regs, NULL); |
83f44ae0 | 2795 | else |
d15d3568 | 2796 | unwind_start(&state, current, NULL, (void *)regs->sp); |
d7d59fb3 | 2797 | |
d15d3568 | 2798 | for (; !unwind_done(&state); unwind_next_frame(&state)) { |
35f4d9b3 JP |
2799 | addr = unwind_get_return_address(&state); |
2800 | if (!addr || perf_callchain_store(entry, addr)) | |
2801 | return; | |
2802 | } | |
d7d59fb3 PZ |
2803 | } |
2804 | ||
bc6ca7b3 AS |
2805 | static inline int |
2806 | valid_user_frame(const void __user *fp, unsigned long size) | |
2807 | { | |
36903abe | 2808 | return __access_ok(fp, size); |
bc6ca7b3 AS |
2809 | } |
2810 | ||
d07bdfd3 PZ |
2811 | static unsigned long get_segment_base(unsigned int segment) |
2812 | { | |
2813 | struct desc_struct *desc; | |
990e9dc3 | 2814 | unsigned int idx = segment >> 3; |
d07bdfd3 PZ |
2815 | |
2816 | if ((segment & SEGMENT_TI_MASK) == SEGMENT_LDT) { | |
a5b9e5a2 | 2817 | #ifdef CONFIG_MODIFY_LDT_SYSCALL |
37868fe1 AL |
2818 | struct ldt_struct *ldt; |
2819 | ||
81e3cbde AL |
2820 | /* |
2821 | * If we're not in a valid context with a real (not just lazy) | |
2822 | * user mm, then don't even try. | |
2823 | */ | |
2824 | if (!nmi_uaccess_okay()) | |
2825 | return 0; | |
2826 | ||
37868fe1 | 2827 | /* IRQs are off, so this synchronizes with smp_store_release */ |
81e3cbde | 2828 | ldt = smp_load_acquire(¤t->mm->context.ldt); |
eaa2f87c | 2829 | if (!ldt || idx >= ldt->nr_entries) |
d07bdfd3 PZ |
2830 | return 0; |
2831 | ||
37868fe1 | 2832 | desc = &ldt->entries[idx]; |
a5b9e5a2 AL |
2833 | #else |
2834 | return 0; | |
2835 | #endif | |
d07bdfd3 | 2836 | } else { |
eaa2f87c | 2837 | if (idx >= GDT_ENTRIES) |
d07bdfd3 PZ |
2838 | return 0; |
2839 | ||
37868fe1 | 2840 | desc = raw_cpu_ptr(gdt_page.gdt) + idx; |
d07bdfd3 PZ |
2841 | } |
2842 | ||
37868fe1 | 2843 | return get_desc_base(desc); |
d07bdfd3 PZ |
2844 | } |
2845 | ||
cfa7f3d2 AN |
2846 | #ifdef CONFIG_UPROBES |
2847 | /* | |
2848 | * Heuristic-based check if uprobe is installed at the function entry. | |
2849 | * | |
2850 | * Under assumption of user code being compiled with frame pointers, | |
2851 | * `push %rbp/%ebp` is a good indicator that we indeed are. | |
2852 | * | |
2853 | * Similarly, `endbr64` (assuming 64-bit mode) is also a common pattern. | |
2854 | * If we get this wrong, captured stack trace might have one extra bogus | |
2855 | * entry, but the rest of stack trace will still be meaningful. | |
2856 | */ | |
2857 | static bool is_uprobe_at_func_entry(struct pt_regs *regs) | |
2858 | { | |
2859 | struct arch_uprobe *auprobe; | |
2860 | ||
2861 | if (!current->utask) | |
2862 | return false; | |
2863 | ||
2864 | auprobe = current->utask->auprobe; | |
2865 | if (!auprobe) | |
2866 | return false; | |
2867 | ||
2868 | /* push %rbp/%ebp */ | |
2869 | if (auprobe->insn[0] == 0x55) | |
2870 | return true; | |
2871 | ||
2872 | /* endbr64 (64-bit only) */ | |
72e213a7 | 2873 | if (user_64bit_mode(regs) && is_endbr((u32 *)auprobe->insn)) |
cfa7f3d2 AN |
2874 | return true; |
2875 | ||
2876 | return false; | |
2877 | } | |
2878 | ||
2879 | #else | |
2880 | static bool is_uprobe_at_func_entry(struct pt_regs *regs) | |
2881 | { | |
2882 | return false; | |
2883 | } | |
2884 | #endif /* CONFIG_UPROBES */ | |
2885 | ||
10ed3493 | 2886 | #ifdef CONFIG_IA32_EMULATION |
d1a797f3 | 2887 | |
0d55303c | 2888 | #include <linux/compat.h> |
d1a797f3 | 2889 | |
257ef9d2 | 2890 | static inline int |
cfbcf468 | 2891 | perf_callchain_user32(struct pt_regs *regs, struct perf_callchain_entry_ctx *entry) |
74193ef0 | 2892 | { |
257ef9d2 | 2893 | /* 32-bit process in 64-bit kernel. */ |
d07bdfd3 | 2894 | unsigned long ss_base, cs_base; |
257ef9d2 | 2895 | struct stack_frame_ia32 frame; |
c8e3dd86 | 2896 | const struct stack_frame_ia32 __user *fp; |
cfa7f3d2 | 2897 | u32 ret_addr; |
74193ef0 | 2898 | |
375d4bfd | 2899 | if (user_64bit_mode(regs)) |
257ef9d2 TE |
2900 | return 0; |
2901 | ||
d07bdfd3 PZ |
2902 | cs_base = get_segment_base(regs->cs); |
2903 | ss_base = get_segment_base(regs->ss); | |
2904 | ||
2905 | fp = compat_ptr(ss_base + regs->bp); | |
75925e1a | 2906 | pagefault_disable(); |
cfa7f3d2 AN |
2907 | |
2908 | /* see perf_callchain_user() below for why we do this */ | |
2909 | if (is_uprobe_at_func_entry(regs) && | |
2910 | !get_user(ret_addr, (const u32 __user *)regs->sp)) | |
2911 | perf_callchain_store(entry, ret_addr); | |
2912 | ||
3b1fff08 | 2913 | while (entry->nr < entry->max_stack) { |
ae31fe51 | 2914 | if (!valid_user_frame(fp, sizeof(frame))) |
75925e1a AK |
2915 | break; |
2916 | ||
c8e3dd86 | 2917 | if (__get_user(frame.next_frame, &fp->next_frame)) |
75925e1a | 2918 | break; |
c8e3dd86 | 2919 | if (__get_user(frame.return_address, &fp->return_address)) |
257ef9d2 | 2920 | break; |
74193ef0 | 2921 | |
d07bdfd3 PZ |
2922 | perf_callchain_store(entry, cs_base + frame.return_address); |
2923 | fp = compat_ptr(ss_base + frame.next_frame); | |
257ef9d2 | 2924 | } |
75925e1a | 2925 | pagefault_enable(); |
257ef9d2 | 2926 | return 1; |
d7d59fb3 | 2927 | } |
257ef9d2 TE |
2928 | #else |
2929 | static inline int | |
cfbcf468 | 2930 | perf_callchain_user32(struct pt_regs *regs, struct perf_callchain_entry_ctx *entry) |
257ef9d2 TE |
2931 | { |
2932 | return 0; | |
2933 | } | |
2934 | #endif | |
d7d59fb3 | 2935 | |
56962b44 | 2936 | void |
cfbcf468 | 2937 | perf_callchain_user(struct perf_callchain_entry_ctx *entry, struct pt_regs *regs) |
d7d59fb3 PZ |
2938 | { |
2939 | struct stack_frame frame; | |
c8e3dd86 | 2940 | const struct stack_frame __user *fp; |
cfa7f3d2 | 2941 | unsigned long ret_addr; |
d7d59fb3 | 2942 | |
1c343051 | 2943 | if (perf_guest_state()) { |
927c7a9e | 2944 | /* TODO: We don't support guest os callchain now */ |
ed805261 | 2945 | return; |
927c7a9e | 2946 | } |
5a6cec3a | 2947 | |
d07bdfd3 PZ |
2948 | /* |
2949 | * We don't know what to do with VM86 stacks.. ignore them for now. | |
2950 | */ | |
2951 | if (regs->flags & (X86_VM_MASK | PERF_EFLAGS_VM)) | |
2952 | return; | |
2953 | ||
c8e3dd86 | 2954 | fp = (void __user *)regs->bp; |
d7d59fb3 | 2955 | |
70791ce9 | 2956 | perf_callchain_store(entry, regs->ip); |
d7d59fb3 | 2957 | |
4012e77a | 2958 | if (!nmi_uaccess_okay()) |
20afc60f AV |
2959 | return; |
2960 | ||
257ef9d2 TE |
2961 | if (perf_callchain_user32(regs, entry)) |
2962 | return; | |
2963 | ||
75925e1a | 2964 | pagefault_disable(); |
cfa7f3d2 AN |
2965 | |
2966 | /* | |
2967 | * If we are called from uprobe handler, and we are indeed at the very | |
2968 | * entry to user function (which is normally a `push %rbp` instruction, | |
2969 | * under assumption of application being compiled with frame pointers), | |
2970 | * we should read return address from *regs->sp before proceeding | |
2971 | * to follow frame pointers, otherwise we'll skip immediate caller | |
2972 | * as %rbp is not yet setup. | |
2973 | */ | |
2974 | if (is_uprobe_at_func_entry(regs) && | |
2975 | !get_user(ret_addr, (const unsigned long __user *)regs->sp)) | |
2976 | perf_callchain_store(entry, ret_addr); | |
2977 | ||
3b1fff08 | 2978 | while (entry->nr < entry->max_stack) { |
ae31fe51 | 2979 | if (!valid_user_frame(fp, sizeof(frame))) |
75925e1a AK |
2980 | break; |
2981 | ||
c8e3dd86 | 2982 | if (__get_user(frame.next_frame, &fp->next_frame)) |
75925e1a | 2983 | break; |
c8e3dd86 | 2984 | if (__get_user(frame.return_address, &fp->return_address)) |
d7d59fb3 PZ |
2985 | break; |
2986 | ||
70791ce9 | 2987 | perf_callchain_store(entry, frame.return_address); |
75925e1a | 2988 | fp = (void __user *)frame.next_frame; |
d7d59fb3 | 2989 | } |
75925e1a | 2990 | pagefault_enable(); |
d7d59fb3 PZ |
2991 | } |
2992 | ||
d07bdfd3 PZ |
2993 | /* |
2994 | * Deal with code segment offsets for the various execution modes: | |
2995 | * | |
2996 | * VM86 - the good olde 16 bit days, where the linear address is | |
2997 | * 20 bits and we use regs->ip + 0x10 * regs->cs. | |
2998 | * | |
2999 | * IA32 - Where we need to look at GDT/LDT segment descriptor tables | |
3000 | * to figure out what the 32bit base address is. | |
3001 | * | |
3002 | * X32 - has TIF_X32 set, but is running in x86_64 | |
3003 | * | |
3004 | * X86_64 - CS,DS,SS,ES are all zero based. | |
3005 | */ | |
3006 | static unsigned long code_segment_base(struct pt_regs *regs) | |
39447b38 | 3007 | { |
383f3af3 AL |
3008 | /* |
3009 | * For IA32 we look at the GDT/LDT segment base to convert the | |
3010 | * effective IP to a linear address. | |
3011 | */ | |
3012 | ||
3013 | #ifdef CONFIG_X86_32 | |
d07bdfd3 PZ |
3014 | /* |
3015 | * If we are in VM86 mode, add the segment offset to convert to a | |
3016 | * linear address. | |
3017 | */ | |
3018 | if (regs->flags & X86_VM_MASK) | |
3019 | return 0x10 * regs->cs; | |
3020 | ||
55474c48 | 3021 | if (user_mode(regs) && regs->cs != __USER_CS) |
d07bdfd3 PZ |
3022 | return get_segment_base(regs->cs); |
3023 | #else | |
c56716af AL |
3024 | if (user_mode(regs) && !user_64bit_mode(regs) && |
3025 | regs->cs != __USER32_CS) | |
3026 | return get_segment_base(regs->cs); | |
d07bdfd3 PZ |
3027 | #endif |
3028 | return 0; | |
3029 | } | |
dcf46b94 | 3030 | |
04782e63 | 3031 | unsigned long perf_arch_instruction_pointer(struct pt_regs *regs) |
d07bdfd3 | 3032 | { |
d07bdfd3 | 3033 | return regs->ip + code_segment_base(regs); |
39447b38 ZY |
3034 | } |
3035 | ||
baff01f3 | 3036 | static unsigned long common_misc_flags(struct pt_regs *regs) |
39447b38 | 3037 | { |
baff01f3 CL |
3038 | if (regs->flags & PERF_EFLAGS_EXACT) |
3039 | return PERF_RECORD_MISC_EXACT_IP; | |
dcf46b94 | 3040 | |
baff01f3 CL |
3041 | return 0; |
3042 | } | |
dcf46b94 | 3043 | |
baff01f3 CL |
3044 | static unsigned long guest_misc_flags(struct pt_regs *regs) |
3045 | { | |
3046 | unsigned long guest_state = perf_guest_state(); | |
3047 | ||
3048 | if (!(guest_state & PERF_GUEST_ACTIVE)) | |
3049 | return 0; | |
3050 | ||
3051 | if (guest_state & PERF_GUEST_USER) | |
3052 | return PERF_RECORD_MISC_GUEST_USER; | |
3053 | else | |
3054 | return PERF_RECORD_MISC_GUEST_KERNEL; | |
3055 | ||
3056 | } | |
3057 | ||
3058 | static unsigned long host_misc_flags(struct pt_regs *regs) | |
3059 | { | |
3060 | if (user_mode(regs)) | |
3061 | return PERF_RECORD_MISC_USER; | |
3062 | else | |
3063 | return PERF_RECORD_MISC_KERNEL; | |
3064 | } | |
3065 | ||
3066 | unsigned long perf_arch_guest_misc_flags(struct pt_regs *regs) | |
3067 | { | |
3068 | unsigned long flags = common_misc_flags(regs); | |
3069 | ||
3070 | flags |= guest_misc_flags(regs); | |
3071 | ||
3072 | return flags; | |
3073 | } | |
3074 | ||
3075 | unsigned long perf_arch_misc_flags(struct pt_regs *regs) | |
3076 | { | |
3077 | unsigned long flags = common_misc_flags(regs); | |
3078 | ||
3079 | flags |= host_misc_flags(regs); | |
39447b38 | 3080 | |
baff01f3 | 3081 | return flags; |
39447b38 | 3082 | } |
b3d9468a GN |
3083 | |
3084 | void perf_get_x86_pmu_capability(struct x86_pmu_capability *cap) | |
3085 | { | |
4b4191b8 SC |
3086 | /* This API doesn't currently support enumerating hybrid PMUs. */ |
3087 | if (WARN_ON_ONCE(cpu_feature_enabled(X86_FEATURE_HYBRID_CPU)) || | |
3088 | !x86_pmu_initialized()) { | |
916e3a4f PB |
3089 | memset(cap, 0, sizeof(*cap)); |
3090 | return; | |
3091 | } | |
3092 | ||
d4b294bf | 3093 | /* |
4b4191b8 SC |
3094 | * Note, hybrid CPU models get tracked as having hybrid PMUs even when |
3095 | * all E-cores are disabled via BIOS. When E-cores are disabled, the | |
3096 | * base PMU holds the correct number of counters for P-cores. | |
d4b294bf | 3097 | */ |
4b4191b8 | 3098 | cap->version = x86_pmu.version; |
722e42e4 KL |
3099 | cap->num_counters_gp = x86_pmu_num_counters(NULL); |
3100 | cap->num_counters_fixed = x86_pmu_num_counters_fixed(NULL); | |
b3d9468a GN |
3101 | cap->bit_width_gp = x86_pmu.cntval_bits; |
3102 | cap->bit_width_fixed = x86_pmu.cntval_bits; | |
3103 | cap->events_mask = (unsigned int)x86_pmu.events_maskl; | |
3104 | cap->events_mask_len = x86_pmu.events_mask_len; | |
fb358e0b | 3105 | cap->pebs_ept = x86_pmu.pebs_ept; |
b3d9468a GN |
3106 | } |
3107 | EXPORT_SYMBOL_GPL(perf_get_x86_pmu_capability); | |
dc852ff5 LX |
3108 | |
3109 | u64 perf_get_hw_event_config(int hw_event) | |
3110 | { | |
3111 | int max = x86_pmu.max_events; | |
3112 | ||
3113 | if (hw_event < max) | |
3114 | return x86_pmu.event_map(array_index_nospec(hw_event, max)); | |
3115 | ||
3116 | return 0; | |
3117 | } | |
3118 | EXPORT_SYMBOL_GPL(perf_get_hw_event_config); |