2 * Performance events x86 architecture code
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
8 * Copyright (C) 2008-2009 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com>
9 * Copyright (C) 2009 Intel Corporation, <markus.t.metzger@intel.com>
10 * Copyright (C) 2009 Google, Inc., Stephane Eranian
12 * For licencing details see kernel-base/COPYING
15 #include <linux/perf_event.h>
16 #include <linux/capability.h>
17 #include <linux/notifier.h>
18 #include <linux/hardirq.h>
19 #include <linux/kprobes.h>
20 #include <linux/module.h>
21 #include <linux/kdebug.h>
22 #include <linux/sched.h>
23 #include <linux/uaccess.h>
24 #include <linux/highmem.h>
25 #include <linux/cpu.h>
26 #include <linux/bitops.h>
29 #include <asm/stacktrace.h>
32 static u64 perf_event_mask __read_mostly
;
34 struct event_constraint
{
36 unsigned long idxmsk
[BITS_TO_LONGS(X86_PMC_IDX_MAX
)];
45 int nb_id
; /* NorthBridge id */
46 int refcnt
; /* reference count */
47 struct perf_event
*owners
[X86_PMC_IDX_MAX
];
48 struct event_constraint event_constraints
[X86_PMC_IDX_MAX
];
51 #define MAX_LBR_ENTRIES 16
53 struct cpu_hw_events
{
55 * Generic x86 PMC bits
57 struct perf_event
*events
[X86_PMC_IDX_MAX
]; /* in counter order */
58 unsigned long active_mask
[BITS_TO_LONGS(X86_PMC_IDX_MAX
)];
59 unsigned long interrupts
;
64 int assign
[X86_PMC_IDX_MAX
]; /* event to counter assignment */
65 u64 tags
[X86_PMC_IDX_MAX
];
66 struct perf_event
*event_list
[X86_PMC_IDX_MAX
]; /* in enabled order */
69 * Intel DebugStore bits
71 struct debug_store
*ds
;
79 struct perf_branch_stack lbr_stack
;
80 struct perf_branch_entry lbr_entries
[MAX_LBR_ENTRIES
];
85 struct amd_nb
*amd_nb
;
88 #define __EVENT_CONSTRAINT(c, n, m, w) {\
89 { .idxmsk64 = (n) }, \
95 #define EVENT_CONSTRAINT(c, n, m) \
96 __EVENT_CONSTRAINT(c, n, m, HWEIGHT(n))
99 * Constraint on the Event code.
101 #define INTEL_EVENT_CONSTRAINT(c, n) \
102 EVENT_CONSTRAINT(c, n, INTEL_ARCH_EVTSEL_MASK)
105 * Constraint on the Event code + UMask + fixed-mask
107 #define FIXED_EVENT_CONSTRAINT(c, n) \
108 EVENT_CONSTRAINT(c, (1ULL << (32+n)), INTEL_ARCH_FIXED_MASK)
111 * Constraint on the Event code + UMask
113 #define PEBS_EVENT_CONSTRAINT(c, n) \
114 EVENT_CONSTRAINT(c, n, INTEL_ARCH_EVENT_MASK)
116 #define EVENT_CONSTRAINT_END \
117 EVENT_CONSTRAINT(0, 0, 0)
119 #define for_each_event_constraint(e, c) \
120 for ((e) = (c); (e)->cmask; (e)++)
123 * struct x86_pmu - generic x86 pmu
127 * Generic x86 PMC bits
131 int (*handle_irq
)(struct pt_regs
*);
132 void (*disable_all
)(void);
133 void (*enable_all
)(void);
134 void (*enable
)(struct perf_event
*);
135 void (*disable
)(struct perf_event
*);
138 u64 (*event_map
)(int);
139 u64 (*raw_event
)(u64
);
142 int num_events_fixed
;
147 struct event_constraint
*
148 (*get_event_constraints
)(struct cpu_hw_events
*cpuc
,
149 struct perf_event
*event
);
151 void (*put_event_constraints
)(struct cpu_hw_events
*cpuc
,
152 struct perf_event
*event
);
153 struct event_constraint
*event_constraints
;
155 void (*cpu_prepare
)(int cpu
);
156 void (*cpu_starting
)(int cpu
);
157 void (*cpu_dying
)(int cpu
);
158 void (*cpu_dead
)(int cpu
);
161 * Intel Arch Perfmon v2+
166 * Intel DebugStore bits
169 int pebs_record_size
;
170 void (*drain_pebs
)(struct pt_regs
*regs
);
171 struct event_constraint
*pebs_constraints
;
176 unsigned long lbr_tos
, lbr_from
, lbr_to
; /* MSR base regs */
177 int lbr_nr
; /* hardware stack size */
178 int lbr_format
; /* hardware format */
181 static struct x86_pmu x86_pmu __read_mostly
;
183 static DEFINE_PER_CPU(struct cpu_hw_events
, cpu_hw_events
) = {
187 static int x86_perf_event_set_period(struct perf_event
*event
);
190 * Generalized hw caching related hw_event table, filled
191 * in on a per model basis. A value of 0 means
192 * 'not supported', -1 means 'hw_event makes no sense on
193 * this CPU', any other value means the raw hw_event
197 #define C(x) PERF_COUNT_HW_CACHE_##x
199 static u64 __read_mostly hw_cache_event_ids
200 [PERF_COUNT_HW_CACHE_MAX
]
201 [PERF_COUNT_HW_CACHE_OP_MAX
]
202 [PERF_COUNT_HW_CACHE_RESULT_MAX
];
205 * Propagate event elapsed time into the generic event.
206 * Can only be executed on the CPU where the event is active.
207 * Returns the delta events processed.
210 x86_perf_event_update(struct perf_event
*event
)
212 struct hw_perf_event
*hwc
= &event
->hw
;
213 int shift
= 64 - x86_pmu
.event_bits
;
214 u64 prev_raw_count
, new_raw_count
;
218 if (idx
== X86_PMC_IDX_FIXED_BTS
)
222 * Careful: an NMI might modify the previous event value.
224 * Our tactic to handle this is to first atomically read and
225 * exchange a new raw count - then add that new-prev delta
226 * count to the generic event atomically:
229 prev_raw_count
= atomic64_read(&hwc
->prev_count
);
230 rdmsrl(hwc
->event_base
+ idx
, new_raw_count
);
232 if (atomic64_cmpxchg(&hwc
->prev_count
, prev_raw_count
,
233 new_raw_count
) != prev_raw_count
)
237 * Now we have the new raw value and have updated the prev
238 * timestamp already. We can now calculate the elapsed delta
239 * (event-)time and add that to the generic event.
241 * Careful, not all hw sign-extends above the physical width
244 delta
= (new_raw_count
<< shift
) - (prev_raw_count
<< shift
);
247 atomic64_add(delta
, &event
->count
);
248 atomic64_sub(delta
, &hwc
->period_left
);
250 return new_raw_count
;
253 static atomic_t active_events
;
254 static DEFINE_MUTEX(pmc_reserve_mutex
);
256 static bool reserve_pmc_hardware(void)
258 #ifdef CONFIG_X86_LOCAL_APIC
261 if (nmi_watchdog
== NMI_LOCAL_APIC
)
262 disable_lapic_nmi_watchdog();
264 for (i
= 0; i
< x86_pmu
.num_events
; i
++) {
265 if (!reserve_perfctr_nmi(x86_pmu
.perfctr
+ i
))
269 for (i
= 0; i
< x86_pmu
.num_events
; i
++) {
270 if (!reserve_evntsel_nmi(x86_pmu
.eventsel
+ i
))
277 #ifdef CONFIG_X86_LOCAL_APIC
279 for (i
--; i
>= 0; i
--)
280 release_evntsel_nmi(x86_pmu
.eventsel
+ i
);
282 i
= x86_pmu
.num_events
;
285 for (i
--; i
>= 0; i
--)
286 release_perfctr_nmi(x86_pmu
.perfctr
+ i
);
288 if (nmi_watchdog
== NMI_LOCAL_APIC
)
289 enable_lapic_nmi_watchdog();
295 static void release_pmc_hardware(void)
297 #ifdef CONFIG_X86_LOCAL_APIC
300 for (i
= 0; i
< x86_pmu
.num_events
; i
++) {
301 release_perfctr_nmi(x86_pmu
.perfctr
+ i
);
302 release_evntsel_nmi(x86_pmu
.eventsel
+ i
);
305 if (nmi_watchdog
== NMI_LOCAL_APIC
)
306 enable_lapic_nmi_watchdog();
310 static int reserve_ds_buffers(void);
311 static void release_ds_buffers(void);
313 static void hw_perf_event_destroy(struct perf_event
*event
)
315 if (atomic_dec_and_mutex_lock(&active_events
, &pmc_reserve_mutex
)) {
316 release_pmc_hardware();
317 release_ds_buffers();
318 mutex_unlock(&pmc_reserve_mutex
);
322 static inline int x86_pmu_initialized(void)
324 return x86_pmu
.handle_irq
!= NULL
;
328 set_ext_hw_attr(struct hw_perf_event
*hwc
, struct perf_event_attr
*attr
)
330 unsigned int cache_type
, cache_op
, cache_result
;
333 config
= attr
->config
;
335 cache_type
= (config
>> 0) & 0xff;
336 if (cache_type
>= PERF_COUNT_HW_CACHE_MAX
)
339 cache_op
= (config
>> 8) & 0xff;
340 if (cache_op
>= PERF_COUNT_HW_CACHE_OP_MAX
)
343 cache_result
= (config
>> 16) & 0xff;
344 if (cache_result
>= PERF_COUNT_HW_CACHE_RESULT_MAX
)
347 val
= hw_cache_event_ids
[cache_type
][cache_op
][cache_result
];
361 * Setup the hardware configuration for a given attr_type
363 static int __hw_perf_event_init(struct perf_event
*event
)
365 struct perf_event_attr
*attr
= &event
->attr
;
366 struct hw_perf_event
*hwc
= &event
->hw
;
370 if (!x86_pmu_initialized())
374 if (!atomic_inc_not_zero(&active_events
)) {
375 mutex_lock(&pmc_reserve_mutex
);
376 if (atomic_read(&active_events
) == 0) {
377 if (!reserve_pmc_hardware())
380 err
= reserve_ds_buffers();
383 atomic_inc(&active_events
);
384 mutex_unlock(&pmc_reserve_mutex
);
389 event
->destroy
= hw_perf_event_destroy
;
393 * (keep 'enabled' bit clear for now)
395 hwc
->config
= ARCH_PERFMON_EVENTSEL_INT
;
399 hwc
->last_tag
= ~0ULL;
402 * Count user and OS events unless requested not to.
404 if (!attr
->exclude_user
)
405 hwc
->config
|= ARCH_PERFMON_EVENTSEL_USR
;
406 if (!attr
->exclude_kernel
)
407 hwc
->config
|= ARCH_PERFMON_EVENTSEL_OS
;
409 if (!hwc
->sample_period
) {
410 hwc
->sample_period
= x86_pmu
.max_period
;
411 hwc
->last_period
= hwc
->sample_period
;
412 atomic64_set(&hwc
->period_left
, hwc
->sample_period
);
415 * If we have a PMU initialized but no APIC
416 * interrupts, we cannot sample hardware
417 * events (user-space has to fall back and
418 * sample via a hrtimer based software event):
425 * Raw hw_event type provide the config in the hw_event structure
427 if (attr
->type
== PERF_TYPE_RAW
) {
428 hwc
->config
|= x86_pmu
.raw_event(attr
->config
);
429 if ((hwc
->config
& ARCH_PERFMON_EVENTSEL_ANY
) &&
430 perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN
))
435 if (attr
->type
== PERF_TYPE_HW_CACHE
)
436 return set_ext_hw_attr(hwc
, attr
);
438 if (attr
->config
>= x86_pmu
.max_events
)
444 config
= x86_pmu
.event_map(attr
->config
);
455 if ((attr
->config
== PERF_COUNT_HW_BRANCH_INSTRUCTIONS
) &&
456 (hwc
->sample_period
== 1)) {
457 /* BTS is not supported by this architecture. */
461 /* BTS is currently only allowed for user-mode. */
462 if (hwc
->config
& ARCH_PERFMON_EVENTSEL_OS
)
466 hwc
->config
|= config
;
471 static void x86_pmu_disable_all(void)
473 struct cpu_hw_events
*cpuc
= &__get_cpu_var(cpu_hw_events
);
476 for (idx
= 0; idx
< x86_pmu
.num_events
; idx
++) {
479 if (!test_bit(idx
, cpuc
->active_mask
))
481 rdmsrl(x86_pmu
.eventsel
+ idx
, val
);
482 if (!(val
& ARCH_PERFMON_EVENTSEL_ENABLE
))
484 val
&= ~ARCH_PERFMON_EVENTSEL_ENABLE
;
485 wrmsrl(x86_pmu
.eventsel
+ idx
, val
);
489 void hw_perf_disable(void)
491 struct cpu_hw_events
*cpuc
= &__get_cpu_var(cpu_hw_events
);
493 if (!x86_pmu_initialized())
503 x86_pmu
.disable_all();
506 static void x86_pmu_enable_all(void)
508 struct cpu_hw_events
*cpuc
= &__get_cpu_var(cpu_hw_events
);
511 for (idx
= 0; idx
< x86_pmu
.num_events
; idx
++) {
512 struct perf_event
*event
= cpuc
->events
[idx
];
515 if (!test_bit(idx
, cpuc
->active_mask
))
518 val
= event
->hw
.config
;
519 val
|= ARCH_PERFMON_EVENTSEL_ENABLE
;
520 wrmsrl(x86_pmu
.eventsel
+ idx
, val
);
524 static const struct pmu pmu
;
526 static inline int is_x86_event(struct perf_event
*event
)
528 return event
->pmu
== &pmu
;
531 static int x86_schedule_events(struct cpu_hw_events
*cpuc
, int n
, int *assign
)
533 struct event_constraint
*c
, *constraints
[X86_PMC_IDX_MAX
];
534 unsigned long used_mask
[BITS_TO_LONGS(X86_PMC_IDX_MAX
)];
535 int i
, j
, w
, wmax
, num
= 0;
536 struct hw_perf_event
*hwc
;
538 bitmap_zero(used_mask
, X86_PMC_IDX_MAX
);
540 for (i
= 0; i
< n
; i
++) {
541 c
= x86_pmu
.get_event_constraints(cpuc
, cpuc
->event_list
[i
]);
546 * fastpath, try to reuse previous register
548 for (i
= 0; i
< n
; i
++) {
549 hwc
= &cpuc
->event_list
[i
]->hw
;
556 /* constraint still honored */
557 if (!test_bit(hwc
->idx
, c
->idxmsk
))
560 /* not already used */
561 if (test_bit(hwc
->idx
, used_mask
))
564 __set_bit(hwc
->idx
, used_mask
);
566 assign
[i
] = hwc
->idx
;
575 bitmap_zero(used_mask
, X86_PMC_IDX_MAX
);
578 * weight = number of possible counters
580 * 1 = most constrained, only works on one counter
581 * wmax = least constrained, works on any counter
583 * assign events to counters starting with most
584 * constrained events.
586 wmax
= x86_pmu
.num_events
;
589 * when fixed event counters are present,
590 * wmax is incremented by 1 to account
591 * for one more choice
593 if (x86_pmu
.num_events_fixed
)
596 for (w
= 1, num
= n
; num
&& w
<= wmax
; w
++) {
598 for (i
= 0; num
&& i
< n
; i
++) {
600 hwc
= &cpuc
->event_list
[i
]->hw
;
605 for_each_set_bit(j
, c
->idxmsk
, X86_PMC_IDX_MAX
) {
606 if (!test_bit(j
, used_mask
))
610 if (j
== X86_PMC_IDX_MAX
)
613 __set_bit(j
, used_mask
);
622 * scheduling failed or is just a simulation,
623 * free resources if necessary
625 if (!assign
|| num
) {
626 for (i
= 0; i
< n
; i
++) {
627 if (x86_pmu
.put_event_constraints
)
628 x86_pmu
.put_event_constraints(cpuc
, cpuc
->event_list
[i
]);
631 return num
? -ENOSPC
: 0;
635 * dogrp: true if must collect siblings events (group)
636 * returns total number of events and error code
638 static int collect_events(struct cpu_hw_events
*cpuc
, struct perf_event
*leader
, bool dogrp
)
640 struct perf_event
*event
;
643 max_count
= x86_pmu
.num_events
+ x86_pmu
.num_events_fixed
;
645 /* current number of events already accepted */
648 if (is_x86_event(leader
)) {
651 cpuc
->event_list
[n
] = leader
;
657 list_for_each_entry(event
, &leader
->sibling_list
, group_entry
) {
658 if (!is_x86_event(event
) ||
659 event
->state
<= PERF_EVENT_STATE_OFF
)
665 cpuc
->event_list
[n
] = event
;
671 static inline void x86_assign_hw_event(struct perf_event
*event
,
672 struct cpu_hw_events
*cpuc
, int i
)
674 struct hw_perf_event
*hwc
= &event
->hw
;
676 hwc
->idx
= cpuc
->assign
[i
];
677 hwc
->last_cpu
= smp_processor_id();
678 hwc
->last_tag
= ++cpuc
->tags
[i
];
680 if (hwc
->idx
== X86_PMC_IDX_FIXED_BTS
) {
681 hwc
->config_base
= 0;
683 } else if (hwc
->idx
>= X86_PMC_IDX_FIXED
) {
684 hwc
->config_base
= MSR_ARCH_PERFMON_FIXED_CTR_CTRL
;
686 * We set it so that event_base + idx in wrmsr/rdmsr maps to
687 * MSR_ARCH_PERFMON_FIXED_CTR0 ... CTR2:
690 MSR_ARCH_PERFMON_FIXED_CTR0
- X86_PMC_IDX_FIXED
;
692 hwc
->config_base
= x86_pmu
.eventsel
;
693 hwc
->event_base
= x86_pmu
.perfctr
;
697 static inline int match_prev_assignment(struct hw_perf_event
*hwc
,
698 struct cpu_hw_events
*cpuc
,
701 return hwc
->idx
== cpuc
->assign
[i
] &&
702 hwc
->last_cpu
== smp_processor_id() &&
703 hwc
->last_tag
== cpuc
->tags
[i
];
706 static int x86_pmu_start(struct perf_event
*event
);
707 static void x86_pmu_stop(struct perf_event
*event
);
709 void hw_perf_enable(void)
711 struct cpu_hw_events
*cpuc
= &__get_cpu_var(cpu_hw_events
);
712 struct perf_event
*event
;
713 struct hw_perf_event
*hwc
;
716 if (!x86_pmu_initialized())
723 int n_running
= cpuc
->n_events
- cpuc
->n_added
;
725 * apply assignment obtained either from
726 * hw_perf_group_sched_in() or x86_pmu_enable()
728 * step1: save events moving to new counters
729 * step2: reprogram moved events into new counters
731 for (i
= 0; i
< n_running
; i
++) {
733 event
= cpuc
->event_list
[i
];
737 * we can avoid reprogramming counter if:
738 * - assigned same counter as last time
739 * - running on same CPU as last time
740 * - no other event has used the counter since
742 if (hwc
->idx
== -1 ||
743 match_prev_assignment(hwc
, cpuc
, i
))
751 for (i
= 0; i
< cpuc
->n_events
; i
++) {
753 event
= cpuc
->event_list
[i
];
757 match_prev_assignment(hwc
, cpuc
, i
))
761 x86_assign_hw_event(event
, cpuc
, i
);
763 x86_pmu_start(event
);
766 perf_events_lapic_init();
772 x86_pmu
.enable_all();
775 static inline void __x86_pmu_enable_event(struct hw_perf_event
*hwc
)
777 (void)checking_wrmsrl(hwc
->config_base
+ hwc
->idx
,
778 hwc
->config
| ARCH_PERFMON_EVENTSEL_ENABLE
);
781 static inline void x86_pmu_disable_event(struct perf_event
*event
)
783 struct hw_perf_event
*hwc
= &event
->hw
;
784 (void)checking_wrmsrl(hwc
->config_base
+ hwc
->idx
, hwc
->config
);
787 static DEFINE_PER_CPU(u64
[X86_PMC_IDX_MAX
], pmc_prev_left
);
790 * Set the next IRQ period, based on the hwc->period_left value.
791 * To be called with the event disabled in hw:
794 x86_perf_event_set_period(struct perf_event
*event
)
796 struct hw_perf_event
*hwc
= &event
->hw
;
797 s64 left
= atomic64_read(&hwc
->period_left
);
798 s64 period
= hwc
->sample_period
;
799 int err
, ret
= 0, idx
= hwc
->idx
;
801 if (idx
== X86_PMC_IDX_FIXED_BTS
)
805 * If we are way outside a reasonable range then just skip forward:
807 if (unlikely(left
<= -period
)) {
809 atomic64_set(&hwc
->period_left
, left
);
810 hwc
->last_period
= period
;
814 if (unlikely(left
<= 0)) {
816 atomic64_set(&hwc
->period_left
, left
);
817 hwc
->last_period
= period
;
821 * Quirk: certain CPUs dont like it if just 1 hw_event is left:
823 if (unlikely(left
< 2))
826 if (left
> x86_pmu
.max_period
)
827 left
= x86_pmu
.max_period
;
829 per_cpu(pmc_prev_left
[idx
], smp_processor_id()) = left
;
832 * The hw event starts counting from this event offset,
833 * mark it to be able to extra future deltas:
835 atomic64_set(&hwc
->prev_count
, (u64
)-left
);
837 err
= checking_wrmsrl(hwc
->event_base
+ idx
,
838 (u64
)(-left
) & x86_pmu
.event_mask
);
840 perf_event_update_userpage(event
);
845 static void x86_pmu_enable_event(struct perf_event
*event
)
847 struct cpu_hw_events
*cpuc
= &__get_cpu_var(cpu_hw_events
);
849 __x86_pmu_enable_event(&event
->hw
);
853 * activate a single event
855 * The event is added to the group of enabled events
856 * but only if it can be scehduled with existing events.
858 * Called with PMU disabled. If successful and return value 1,
859 * then guaranteed to call perf_enable() and hw_perf_enable()
861 static int x86_pmu_enable(struct perf_event
*event
)
863 struct cpu_hw_events
*cpuc
= &__get_cpu_var(cpu_hw_events
);
864 struct hw_perf_event
*hwc
;
865 int assign
[X86_PMC_IDX_MAX
];
871 n
= collect_events(cpuc
, event
, false);
875 ret
= x86_schedule_events(cpuc
, n
, assign
);
879 * copy new assignment, now we know it is possible
880 * will be used by hw_perf_enable()
882 memcpy(cpuc
->assign
, assign
, n
*sizeof(int));
885 cpuc
->n_added
+= n
- n0
;
890 static int x86_pmu_start(struct perf_event
*event
)
892 struct cpu_hw_events
*cpuc
= &__get_cpu_var(cpu_hw_events
);
893 int idx
= event
->hw
.idx
;
898 x86_perf_event_set_period(event
);
899 cpuc
->events
[idx
] = event
;
900 __set_bit(idx
, cpuc
->active_mask
);
901 x86_pmu
.enable(event
);
902 perf_event_update_userpage(event
);
907 static void x86_pmu_unthrottle(struct perf_event
*event
)
909 int ret
= x86_pmu_start(event
);
913 void perf_event_print_debug(void)
915 u64 ctrl
, status
, overflow
, pmc_ctrl
, pmc_count
, prev_left
, fixed
;
917 struct cpu_hw_events
*cpuc
;
921 if (!x86_pmu
.num_events
)
924 local_irq_save(flags
);
926 cpu
= smp_processor_id();
927 cpuc
= &per_cpu(cpu_hw_events
, cpu
);
929 if (x86_pmu
.version
>= 2) {
930 rdmsrl(MSR_CORE_PERF_GLOBAL_CTRL
, ctrl
);
931 rdmsrl(MSR_CORE_PERF_GLOBAL_STATUS
, status
);
932 rdmsrl(MSR_CORE_PERF_GLOBAL_OVF_CTRL
, overflow
);
933 rdmsrl(MSR_ARCH_PERFMON_FIXED_CTR_CTRL
, fixed
);
934 rdmsrl(MSR_IA32_PEBS_ENABLE
, pebs
);
937 pr_info("CPU#%d: ctrl: %016llx\n", cpu
, ctrl
);
938 pr_info("CPU#%d: status: %016llx\n", cpu
, status
);
939 pr_info("CPU#%d: overflow: %016llx\n", cpu
, overflow
);
940 pr_info("CPU#%d: fixed: %016llx\n", cpu
, fixed
);
941 pr_info("CPU#%d: pebs: %016llx\n", cpu
, pebs
);
943 pr_info("CPU#%d: active: %016llx\n", cpu
, *(u64
*)cpuc
->active_mask
);
945 for (idx
= 0; idx
< x86_pmu
.num_events
; idx
++) {
946 rdmsrl(x86_pmu
.eventsel
+ idx
, pmc_ctrl
);
947 rdmsrl(x86_pmu
.perfctr
+ idx
, pmc_count
);
949 prev_left
= per_cpu(pmc_prev_left
[idx
], cpu
);
951 pr_info("CPU#%d: gen-PMC%d ctrl: %016llx\n",
953 pr_info("CPU#%d: gen-PMC%d count: %016llx\n",
954 cpu
, idx
, pmc_count
);
955 pr_info("CPU#%d: gen-PMC%d left: %016llx\n",
956 cpu
, idx
, prev_left
);
958 for (idx
= 0; idx
< x86_pmu
.num_events_fixed
; idx
++) {
959 rdmsrl(MSR_ARCH_PERFMON_FIXED_CTR0
+ idx
, pmc_count
);
961 pr_info("CPU#%d: fixed-PMC%d count: %016llx\n",
962 cpu
, idx
, pmc_count
);
964 local_irq_restore(flags
);
967 static void x86_pmu_stop(struct perf_event
*event
)
969 struct cpu_hw_events
*cpuc
= &__get_cpu_var(cpu_hw_events
);
970 struct hw_perf_event
*hwc
= &event
->hw
;
973 if (!__test_and_clear_bit(idx
, cpuc
->active_mask
))
976 x86_pmu
.disable(event
);
979 * Drain the remaining delta count out of a event
980 * that we are disabling:
982 x86_perf_event_update(event
);
984 cpuc
->events
[idx
] = NULL
;
987 static void x86_pmu_disable(struct perf_event
*event
)
989 struct cpu_hw_events
*cpuc
= &__get_cpu_var(cpu_hw_events
);
994 for (i
= 0; i
< cpuc
->n_events
; i
++) {
995 if (event
== cpuc
->event_list
[i
]) {
997 if (x86_pmu
.put_event_constraints
)
998 x86_pmu
.put_event_constraints(cpuc
, event
);
1000 while (++i
< cpuc
->n_events
)
1001 cpuc
->event_list
[i
-1] = cpuc
->event_list
[i
];
1007 perf_event_update_userpage(event
);
1010 static int x86_pmu_handle_irq(struct pt_regs
*regs
)
1012 struct perf_sample_data data
;
1013 struct cpu_hw_events
*cpuc
;
1014 struct perf_event
*event
;
1015 struct hw_perf_event
*hwc
;
1016 int idx
, handled
= 0;
1019 perf_sample_data_init(&data
, 0);
1021 cpuc
= &__get_cpu_var(cpu_hw_events
);
1023 for (idx
= 0; idx
< x86_pmu
.num_events
; idx
++) {
1024 if (!test_bit(idx
, cpuc
->active_mask
))
1027 event
= cpuc
->events
[idx
];
1030 val
= x86_perf_event_update(event
);
1031 if (val
& (1ULL << (x86_pmu
.event_bits
- 1)))
1038 data
.period
= event
->hw
.last_period
;
1040 if (!x86_perf_event_set_period(event
))
1043 if (perf_event_overflow(event
, 1, &data
, regs
))
1044 x86_pmu_stop(event
);
1048 inc_irq_stat(apic_perf_irqs
);
1053 void smp_perf_pending_interrupt(struct pt_regs
*regs
)
1057 inc_irq_stat(apic_pending_irqs
);
1058 perf_event_do_pending();
1062 void set_perf_event_pending(void)
1064 #ifdef CONFIG_X86_LOCAL_APIC
1065 if (!x86_pmu
.apic
|| !x86_pmu_initialized())
1068 apic
->send_IPI_self(LOCAL_PENDING_VECTOR
);
1072 void perf_events_lapic_init(void)
1074 #ifdef CONFIG_X86_LOCAL_APIC
1075 if (!x86_pmu
.apic
|| !x86_pmu_initialized())
1079 * Always use NMI for PMU
1081 apic_write(APIC_LVTPC
, APIC_DM_NMI
);
1085 static int __kprobes
1086 perf_event_nmi_handler(struct notifier_block
*self
,
1087 unsigned long cmd
, void *__args
)
1089 struct die_args
*args
= __args
;
1090 struct pt_regs
*regs
;
1092 if (!atomic_read(&active_events
))
1106 #ifdef CONFIG_X86_LOCAL_APIC
1107 apic_write(APIC_LVTPC
, APIC_DM_NMI
);
1110 * Can't rely on the handled return value to say it was our NMI, two
1111 * events could trigger 'simultaneously' raising two back-to-back NMIs.
1113 * If the first NMI handles both, the latter will be empty and daze
1116 x86_pmu
.handle_irq(regs
);
1121 static __read_mostly
struct notifier_block perf_event_nmi_notifier
= {
1122 .notifier_call
= perf_event_nmi_handler
,
1127 static struct event_constraint unconstrained
;
1128 static struct event_constraint emptyconstraint
;
1130 static struct event_constraint
*
1131 x86_get_event_constraints(struct cpu_hw_events
*cpuc
, struct perf_event
*event
)
1133 struct event_constraint
*c
;
1135 if (x86_pmu
.event_constraints
) {
1136 for_each_event_constraint(c
, x86_pmu
.event_constraints
) {
1137 if ((event
->hw
.config
& c
->cmask
) == c
->code
)
1142 return &unconstrained
;
1145 static int x86_event_sched_in(struct perf_event
*event
,
1146 struct perf_cpu_context
*cpuctx
)
1150 event
->state
= PERF_EVENT_STATE_ACTIVE
;
1151 event
->oncpu
= smp_processor_id();
1152 event
->tstamp_running
+= event
->ctx
->time
- event
->tstamp_stopped
;
1154 if (!is_x86_event(event
))
1155 ret
= event
->pmu
->enable(event
);
1157 if (!ret
&& !is_software_event(event
))
1158 cpuctx
->active_oncpu
++;
1160 if (!ret
&& event
->attr
.exclusive
)
1161 cpuctx
->exclusive
= 1;
1166 static void x86_event_sched_out(struct perf_event
*event
,
1167 struct perf_cpu_context
*cpuctx
)
1169 event
->state
= PERF_EVENT_STATE_INACTIVE
;
1172 if (!is_x86_event(event
))
1173 event
->pmu
->disable(event
);
1175 event
->tstamp_running
-= event
->ctx
->time
- event
->tstamp_stopped
;
1177 if (!is_software_event(event
))
1178 cpuctx
->active_oncpu
--;
1180 if (event
->attr
.exclusive
|| !cpuctx
->active_oncpu
)
1181 cpuctx
->exclusive
= 0;
1185 * Called to enable a whole group of events.
1186 * Returns 1 if the group was enabled, or -EAGAIN if it could not be.
1187 * Assumes the caller has disabled interrupts and has
1188 * frozen the PMU with hw_perf_save_disable.
1190 * called with PMU disabled. If successful and return value 1,
1191 * then guaranteed to call perf_enable() and hw_perf_enable()
1193 int hw_perf_group_sched_in(struct perf_event
*leader
,
1194 struct perf_cpu_context
*cpuctx
,
1195 struct perf_event_context
*ctx
)
1197 struct cpu_hw_events
*cpuc
= &__get_cpu_var(cpu_hw_events
);
1198 struct perf_event
*sub
;
1199 int assign
[X86_PMC_IDX_MAX
];
1202 /* n0 = total number of events */
1203 n0
= collect_events(cpuc
, leader
, true);
1207 ret
= x86_schedule_events(cpuc
, n0
, assign
);
1211 ret
= x86_event_sched_in(leader
, cpuctx
);
1216 list_for_each_entry(sub
, &leader
->sibling_list
, group_entry
) {
1217 if (sub
->state
> PERF_EVENT_STATE_OFF
) {
1218 ret
= x86_event_sched_in(sub
, cpuctx
);
1225 * copy new assignment, now we know it is possible
1226 * will be used by hw_perf_enable()
1228 memcpy(cpuc
->assign
, assign
, n0
*sizeof(int));
1230 cpuc
->n_events
= n0
;
1231 cpuc
->n_added
+= n1
;
1232 ctx
->nr_active
+= n1
;
1235 * 1 means successful and events are active
1236 * This is not quite true because we defer
1237 * actual activation until hw_perf_enable() but
1238 * this way we* ensure caller won't try to enable
1243 x86_event_sched_out(leader
, cpuctx
);
1245 list_for_each_entry(sub
, &leader
->sibling_list
, group_entry
) {
1246 if (sub
->state
== PERF_EVENT_STATE_ACTIVE
) {
1247 x86_event_sched_out(sub
, cpuctx
);
1255 #include "perf_event_amd.c"
1256 #include "perf_event_p6.c"
1257 #include "perf_event_intel_lbr.c"
1258 #include "perf_event_intel_ds.c"
1259 #include "perf_event_intel.c"
1261 static int __cpuinit
1262 x86_pmu_notifier(struct notifier_block
*self
, unsigned long action
, void *hcpu
)
1264 unsigned int cpu
= (long)hcpu
;
1266 switch (action
& ~CPU_TASKS_FROZEN
) {
1267 case CPU_UP_PREPARE
:
1268 if (x86_pmu
.cpu_prepare
)
1269 x86_pmu
.cpu_prepare(cpu
);
1273 if (x86_pmu
.cpu_starting
)
1274 x86_pmu
.cpu_starting(cpu
);
1278 if (x86_pmu
.cpu_dying
)
1279 x86_pmu
.cpu_dying(cpu
);
1283 if (x86_pmu
.cpu_dead
)
1284 x86_pmu
.cpu_dead(cpu
);
1294 static void __init
pmu_check_apic(void)
1300 pr_info("no APIC, boot with the \"lapic\" boot parameter to force-enable it.\n");
1301 pr_info("no hardware sampling interrupt available.\n");
1304 void __init
init_hw_perf_events(void)
1306 struct event_constraint
*c
;
1309 pr_info("Performance Events: ");
1311 switch (boot_cpu_data
.x86_vendor
) {
1312 case X86_VENDOR_INTEL
:
1313 err
= intel_pmu_init();
1315 case X86_VENDOR_AMD
:
1316 err
= amd_pmu_init();
1322 pr_cont("no PMU driver, software events only.\n");
1328 pr_cont("%s PMU driver.\n", x86_pmu
.name
);
1330 if (x86_pmu
.num_events
> X86_PMC_MAX_GENERIC
) {
1331 WARN(1, KERN_ERR
"hw perf events %d > max(%d), clipping!",
1332 x86_pmu
.num_events
, X86_PMC_MAX_GENERIC
);
1333 x86_pmu
.num_events
= X86_PMC_MAX_GENERIC
;
1335 perf_event_mask
= (1 << x86_pmu
.num_events
) - 1;
1336 perf_max_events
= x86_pmu
.num_events
;
1338 if (x86_pmu
.num_events_fixed
> X86_PMC_MAX_FIXED
) {
1339 WARN(1, KERN_ERR
"hw perf events fixed %d > max(%d), clipping!",
1340 x86_pmu
.num_events_fixed
, X86_PMC_MAX_FIXED
);
1341 x86_pmu
.num_events_fixed
= X86_PMC_MAX_FIXED
;
1345 ((1LL << x86_pmu
.num_events_fixed
)-1) << X86_PMC_IDX_FIXED
;
1346 x86_pmu
.intel_ctrl
= perf_event_mask
;
1348 perf_events_lapic_init();
1349 register_die_notifier(&perf_event_nmi_notifier
);
1351 unconstrained
= (struct event_constraint
)
1352 __EVENT_CONSTRAINT(0, (1ULL << x86_pmu
.num_events
) - 1,
1353 0, x86_pmu
.num_events
);
1355 if (x86_pmu
.event_constraints
) {
1356 for_each_event_constraint(c
, x86_pmu
.event_constraints
) {
1357 if (c
->cmask
!= INTEL_ARCH_FIXED_MASK
)
1360 c
->idxmsk64
|= (1ULL << x86_pmu
.num_events
) - 1;
1361 c
->weight
+= x86_pmu
.num_events
;
1365 pr_info("... version: %d\n", x86_pmu
.version
);
1366 pr_info("... bit width: %d\n", x86_pmu
.event_bits
);
1367 pr_info("... generic registers: %d\n", x86_pmu
.num_events
);
1368 pr_info("... value mask: %016Lx\n", x86_pmu
.event_mask
);
1369 pr_info("... max period: %016Lx\n", x86_pmu
.max_period
);
1370 pr_info("... fixed-purpose events: %d\n", x86_pmu
.num_events_fixed
);
1371 pr_info("... event mask: %016Lx\n", perf_event_mask
);
1373 perf_cpu_notifier(x86_pmu_notifier
);
1376 static inline void x86_pmu_read(struct perf_event
*event
)
1378 x86_perf_event_update(event
);
1381 static const struct pmu pmu
= {
1382 .enable
= x86_pmu_enable
,
1383 .disable
= x86_pmu_disable
,
1384 .start
= x86_pmu_start
,
1385 .stop
= x86_pmu_stop
,
1386 .read
= x86_pmu_read
,
1387 .unthrottle
= x86_pmu_unthrottle
,
1391 * validate that we can schedule this event
1393 static int validate_event(struct perf_event
*event
)
1395 struct cpu_hw_events
*fake_cpuc
;
1396 struct event_constraint
*c
;
1399 fake_cpuc
= kmalloc(sizeof(*fake_cpuc
), GFP_KERNEL
| __GFP_ZERO
);
1403 c
= x86_pmu
.get_event_constraints(fake_cpuc
, event
);
1405 if (!c
|| !c
->weight
)
1408 if (x86_pmu
.put_event_constraints
)
1409 x86_pmu
.put_event_constraints(fake_cpuc
, event
);
1417 * validate a single event group
1419 * validation include:
1420 * - check events are compatible which each other
1421 * - events do not compete for the same counter
1422 * - number of events <= number of counters
1424 * validation ensures the group can be loaded onto the
1425 * PMU if it was the only group available.
1427 static int validate_group(struct perf_event
*event
)
1429 struct perf_event
*leader
= event
->group_leader
;
1430 struct cpu_hw_events
*fake_cpuc
;
1434 fake_cpuc
= kmalloc(sizeof(*fake_cpuc
), GFP_KERNEL
| __GFP_ZERO
);
1439 * the event is not yet connected with its
1440 * siblings therefore we must first collect
1441 * existing siblings, then add the new event
1442 * before we can simulate the scheduling
1445 n
= collect_events(fake_cpuc
, leader
, true);
1449 fake_cpuc
->n_events
= n
;
1450 n
= collect_events(fake_cpuc
, event
, false);
1454 fake_cpuc
->n_events
= n
;
1456 ret
= x86_schedule_events(fake_cpuc
, n
, NULL
);
1464 const struct pmu
*hw_perf_event_init(struct perf_event
*event
)
1466 const struct pmu
*tmp
;
1469 err
= __hw_perf_event_init(event
);
1472 * we temporarily connect event to its pmu
1473 * such that validate_group() can classify
1474 * it as an x86 event using is_x86_event()
1479 if (event
->group_leader
!= event
)
1480 err
= validate_group(event
);
1482 err
= validate_event(event
);
1488 event
->destroy(event
);
1489 return ERR_PTR(err
);
1500 void callchain_store(struct perf_callchain_entry
*entry
, u64 ip
)
1502 if (entry
->nr
< PERF_MAX_STACK_DEPTH
)
1503 entry
->ip
[entry
->nr
++] = ip
;
1506 static DEFINE_PER_CPU(struct perf_callchain_entry
, pmc_irq_entry
);
1507 static DEFINE_PER_CPU(struct perf_callchain_entry
, pmc_nmi_entry
);
1511 backtrace_warning_symbol(void *data
, char *msg
, unsigned long symbol
)
1513 /* Ignore warnings */
1516 static void backtrace_warning(void *data
, char *msg
)
1518 /* Ignore warnings */
1521 static int backtrace_stack(void *data
, char *name
)
1526 static void backtrace_address(void *data
, unsigned long addr
, int reliable
)
1528 struct perf_callchain_entry
*entry
= data
;
1531 callchain_store(entry
, addr
);
1534 static const struct stacktrace_ops backtrace_ops
= {
1535 .warning
= backtrace_warning
,
1536 .warning_symbol
= backtrace_warning_symbol
,
1537 .stack
= backtrace_stack
,
1538 .address
= backtrace_address
,
1539 .walk_stack
= print_context_stack_bp
,
1542 #include "../dumpstack.h"
1545 perf_callchain_kernel(struct pt_regs
*regs
, struct perf_callchain_entry
*entry
)
1547 callchain_store(entry
, PERF_CONTEXT_KERNEL
);
1548 callchain_store(entry
, regs
->ip
);
1550 dump_trace(NULL
, regs
, NULL
, regs
->bp
, &backtrace_ops
, entry
);
1554 * best effort, GUP based copy_from_user() that assumes IRQ or NMI context
1556 static unsigned long
1557 copy_from_user_nmi(void *to
, const void __user
*from
, unsigned long n
)
1559 unsigned long offset
, addr
= (unsigned long)from
;
1560 int type
= in_nmi() ? KM_NMI
: KM_IRQ0
;
1561 unsigned long size
, len
= 0;
1567 ret
= __get_user_pages_fast(addr
, 1, 0, &page
);
1571 offset
= addr
& (PAGE_SIZE
- 1);
1572 size
= min(PAGE_SIZE
- offset
, n
- len
);
1574 map
= kmap_atomic(page
, type
);
1575 memcpy(to
, map
+offset
, size
);
1576 kunmap_atomic(map
, type
);
1588 static int copy_stack_frame(const void __user
*fp
, struct stack_frame
*frame
)
1590 unsigned long bytes
;
1592 bytes
= copy_from_user_nmi(frame
, fp
, sizeof(*frame
));
1594 return bytes
== sizeof(*frame
);
1598 perf_callchain_user(struct pt_regs
*regs
, struct perf_callchain_entry
*entry
)
1600 struct stack_frame frame
;
1601 const void __user
*fp
;
1603 if (!user_mode(regs
))
1604 regs
= task_pt_regs(current
);
1606 fp
= (void __user
*)regs
->bp
;
1608 callchain_store(entry
, PERF_CONTEXT_USER
);
1609 callchain_store(entry
, regs
->ip
);
1611 while (entry
->nr
< PERF_MAX_STACK_DEPTH
) {
1612 frame
.next_frame
= NULL
;
1613 frame
.return_address
= 0;
1615 if (!copy_stack_frame(fp
, &frame
))
1618 if ((unsigned long)fp
< regs
->sp
)
1621 callchain_store(entry
, frame
.return_address
);
1622 fp
= frame
.next_frame
;
1627 perf_do_callchain(struct pt_regs
*regs
, struct perf_callchain_entry
*entry
)
1634 is_user
= user_mode(regs
);
1636 if (is_user
&& current
->state
!= TASK_RUNNING
)
1640 perf_callchain_kernel(regs
, entry
);
1643 perf_callchain_user(regs
, entry
);
1646 struct perf_callchain_entry
*perf_callchain(struct pt_regs
*regs
)
1648 struct perf_callchain_entry
*entry
;
1651 entry
= &__get_cpu_var(pmc_nmi_entry
);
1653 entry
= &__get_cpu_var(pmc_irq_entry
);
1657 perf_do_callchain(regs
, entry
);