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/slab.h>
25 #include <linux/highmem.h>
26 #include <linux/cpu.h>
27 #include <linux/bitops.h>
30 #include <asm/stacktrace.h>
32 #include <asm/compat.h>
36 #define wrmsrl(msr, val) \
38 trace_printk("wrmsrl(%lx, %lx)\n", (unsigned long)(msr),\
39 (unsigned long)(val)); \
40 native_write_msr((msr), (u32)((u64)(val)), \
41 (u32)((u64)(val) >> 32)); \
46 * best effort, GUP based copy_from_user() that assumes IRQ or NMI context
49 copy_from_user_nmi(void *to
, const void __user
*from
, unsigned long n
)
51 unsigned long offset
, addr
= (unsigned long)from
;
52 int type
= in_nmi() ? KM_NMI
: KM_IRQ0
;
53 unsigned long size
, len
= 0;
59 ret
= __get_user_pages_fast(addr
, 1, 0, &page
);
63 offset
= addr
& (PAGE_SIZE
- 1);
64 size
= min(PAGE_SIZE
- offset
, n
- len
);
66 map
= kmap_atomic(page
, type
);
67 memcpy(to
, map
+offset
, size
);
68 kunmap_atomic(map
, type
);
80 struct event_constraint
{
82 unsigned long idxmsk
[BITS_TO_LONGS(X86_PMC_IDX_MAX
)];
91 int nb_id
; /* NorthBridge id */
92 int refcnt
; /* reference count */
93 struct perf_event
*owners
[X86_PMC_IDX_MAX
];
94 struct event_constraint event_constraints
[X86_PMC_IDX_MAX
];
97 #define MAX_LBR_ENTRIES 16
99 struct cpu_hw_events
{
101 * Generic x86 PMC bits
103 struct perf_event
*events
[X86_PMC_IDX_MAX
]; /* in counter order */
104 unsigned long active_mask
[BITS_TO_LONGS(X86_PMC_IDX_MAX
)];
110 int assign
[X86_PMC_IDX_MAX
]; /* event to counter assignment */
111 u64 tags
[X86_PMC_IDX_MAX
];
112 struct perf_event
*event_list
[X86_PMC_IDX_MAX
]; /* in enabled order */
114 unsigned int group_flag
;
117 * Intel DebugStore bits
119 struct debug_store
*ds
;
127 struct perf_branch_stack lbr_stack
;
128 struct perf_branch_entry lbr_entries
[MAX_LBR_ENTRIES
];
133 struct amd_nb
*amd_nb
;
136 #define __EVENT_CONSTRAINT(c, n, m, w) {\
137 { .idxmsk64 = (n) }, \
143 #define EVENT_CONSTRAINT(c, n, m) \
144 __EVENT_CONSTRAINT(c, n, m, HWEIGHT(n))
147 * Constraint on the Event code.
149 #define INTEL_EVENT_CONSTRAINT(c, n) \
150 EVENT_CONSTRAINT(c, n, ARCH_PERFMON_EVENTSEL_EVENT)
153 * Constraint on the Event code + UMask + fixed-mask
155 * filter mask to validate fixed counter events.
156 * the following filters disqualify for fixed counters:
160 * The other filters are supported by fixed counters.
161 * The any-thread option is supported starting with v3.
163 #define FIXED_EVENT_CONSTRAINT(c, n) \
164 EVENT_CONSTRAINT(c, (1ULL << (32+n)), X86_RAW_EVENT_MASK)
167 * Constraint on the Event code + UMask
169 #define PEBS_EVENT_CONSTRAINT(c, n) \
170 EVENT_CONSTRAINT(c, n, INTEL_ARCH_EVENT_MASK)
172 #define EVENT_CONSTRAINT_END \
173 EVENT_CONSTRAINT(0, 0, 0)
175 #define for_each_event_constraint(e, c) \
176 for ((e) = (c); (e)->weight; (e)++)
178 union perf_capabilities
{
182 u64 pebs_arch_reg
: 1;
190 * struct x86_pmu - generic x86 pmu
194 * Generic x86 PMC bits
198 int (*handle_irq
)(struct pt_regs
*);
199 void (*disable_all
)(void);
200 void (*enable_all
)(int added
);
201 void (*enable
)(struct perf_event
*);
202 void (*disable
)(struct perf_event
*);
203 int (*hw_config
)(struct perf_event
*event
);
204 int (*schedule_events
)(struct cpu_hw_events
*cpuc
, int n
, int *assign
);
207 u64 (*event_map
)(int);
210 int num_counters_fixed
;
215 struct event_constraint
*
216 (*get_event_constraints
)(struct cpu_hw_events
*cpuc
,
217 struct perf_event
*event
);
219 void (*put_event_constraints
)(struct cpu_hw_events
*cpuc
,
220 struct perf_event
*event
);
221 struct event_constraint
*event_constraints
;
222 void (*quirks
)(void);
224 int (*cpu_prepare
)(int cpu
);
225 void (*cpu_starting
)(int cpu
);
226 void (*cpu_dying
)(int cpu
);
227 void (*cpu_dead
)(int cpu
);
230 * Intel Arch Perfmon v2+
233 union perf_capabilities intel_cap
;
236 * Intel DebugStore bits
239 int pebs_record_size
;
240 void (*drain_pebs
)(struct pt_regs
*regs
);
241 struct event_constraint
*pebs_constraints
;
246 unsigned long lbr_tos
, lbr_from
, lbr_to
; /* MSR base regs */
247 int lbr_nr
; /* hardware stack size */
250 static struct x86_pmu x86_pmu __read_mostly
;
252 static DEFINE_PER_CPU(struct cpu_hw_events
, cpu_hw_events
) = {
256 static int x86_perf_event_set_period(struct perf_event
*event
);
259 * Generalized hw caching related hw_event table, filled
260 * in on a per model basis. A value of 0 means
261 * 'not supported', -1 means 'hw_event makes no sense on
262 * this CPU', any other value means the raw hw_event
266 #define C(x) PERF_COUNT_HW_CACHE_##x
268 static u64 __read_mostly hw_cache_event_ids
269 [PERF_COUNT_HW_CACHE_MAX
]
270 [PERF_COUNT_HW_CACHE_OP_MAX
]
271 [PERF_COUNT_HW_CACHE_RESULT_MAX
];
274 * Propagate event elapsed time into the generic event.
275 * Can only be executed on the CPU where the event is active.
276 * Returns the delta events processed.
279 x86_perf_event_update(struct perf_event
*event
)
281 struct hw_perf_event
*hwc
= &event
->hw
;
282 int shift
= 64 - x86_pmu
.cntval_bits
;
283 u64 prev_raw_count
, new_raw_count
;
287 if (idx
== X86_PMC_IDX_FIXED_BTS
)
291 * Careful: an NMI might modify the previous event value.
293 * Our tactic to handle this is to first atomically read and
294 * exchange a new raw count - then add that new-prev delta
295 * count to the generic event atomically:
298 prev_raw_count
= atomic64_read(&hwc
->prev_count
);
299 rdmsrl(hwc
->event_base
+ idx
, new_raw_count
);
301 if (atomic64_cmpxchg(&hwc
->prev_count
, prev_raw_count
,
302 new_raw_count
) != prev_raw_count
)
306 * Now we have the new raw value and have updated the prev
307 * timestamp already. We can now calculate the elapsed delta
308 * (event-)time and add that to the generic event.
310 * Careful, not all hw sign-extends above the physical width
313 delta
= (new_raw_count
<< shift
) - (prev_raw_count
<< shift
);
316 atomic64_add(delta
, &event
->count
);
317 atomic64_sub(delta
, &hwc
->period_left
);
319 return new_raw_count
;
322 static atomic_t active_events
;
323 static DEFINE_MUTEX(pmc_reserve_mutex
);
325 #ifdef CONFIG_X86_LOCAL_APIC
327 static bool reserve_pmc_hardware(void)
331 if (nmi_watchdog
== NMI_LOCAL_APIC
)
332 disable_lapic_nmi_watchdog();
334 for (i
= 0; i
< x86_pmu
.num_counters
; i
++) {
335 if (!reserve_perfctr_nmi(x86_pmu
.perfctr
+ i
))
339 for (i
= 0; i
< x86_pmu
.num_counters
; i
++) {
340 if (!reserve_evntsel_nmi(x86_pmu
.eventsel
+ i
))
347 for (i
--; i
>= 0; i
--)
348 release_evntsel_nmi(x86_pmu
.eventsel
+ i
);
350 i
= x86_pmu
.num_counters
;
353 for (i
--; i
>= 0; i
--)
354 release_perfctr_nmi(x86_pmu
.perfctr
+ i
);
356 if (nmi_watchdog
== NMI_LOCAL_APIC
)
357 enable_lapic_nmi_watchdog();
362 static void release_pmc_hardware(void)
366 for (i
= 0; i
< x86_pmu
.num_counters
; i
++) {
367 release_perfctr_nmi(x86_pmu
.perfctr
+ i
);
368 release_evntsel_nmi(x86_pmu
.eventsel
+ i
);
371 if (nmi_watchdog
== NMI_LOCAL_APIC
)
372 enable_lapic_nmi_watchdog();
377 static bool reserve_pmc_hardware(void) { return true; }
378 static void release_pmc_hardware(void) {}
382 static int reserve_ds_buffers(void);
383 static void release_ds_buffers(void);
385 static void hw_perf_event_destroy(struct perf_event
*event
)
387 if (atomic_dec_and_mutex_lock(&active_events
, &pmc_reserve_mutex
)) {
388 release_pmc_hardware();
389 release_ds_buffers();
390 mutex_unlock(&pmc_reserve_mutex
);
394 static inline int x86_pmu_initialized(void)
396 return x86_pmu
.handle_irq
!= NULL
;
400 set_ext_hw_attr(struct hw_perf_event
*hwc
, struct perf_event_attr
*attr
)
402 unsigned int cache_type
, cache_op
, cache_result
;
405 config
= attr
->config
;
407 cache_type
= (config
>> 0) & 0xff;
408 if (cache_type
>= PERF_COUNT_HW_CACHE_MAX
)
411 cache_op
= (config
>> 8) & 0xff;
412 if (cache_op
>= PERF_COUNT_HW_CACHE_OP_MAX
)
415 cache_result
= (config
>> 16) & 0xff;
416 if (cache_result
>= PERF_COUNT_HW_CACHE_RESULT_MAX
)
419 val
= hw_cache_event_ids
[cache_type
][cache_op
][cache_result
];
432 static int x86_setup_perfctr(struct perf_event
*event
)
434 struct perf_event_attr
*attr
= &event
->attr
;
435 struct hw_perf_event
*hwc
= &event
->hw
;
438 if (!hwc
->sample_period
) {
439 hwc
->sample_period
= x86_pmu
.max_period
;
440 hwc
->last_period
= hwc
->sample_period
;
441 atomic64_set(&hwc
->period_left
, hwc
->sample_period
);
444 * If we have a PMU initialized but no APIC
445 * interrupts, we cannot sample hardware
446 * events (user-space has to fall back and
447 * sample via a hrtimer based software event):
453 if (attr
->type
== PERF_TYPE_RAW
)
456 if (attr
->type
== PERF_TYPE_HW_CACHE
)
457 return set_ext_hw_attr(hwc
, attr
);
459 if (attr
->config
>= x86_pmu
.max_events
)
465 config
= x86_pmu
.event_map(attr
->config
);
476 if ((attr
->config
== PERF_COUNT_HW_BRANCH_INSTRUCTIONS
) &&
477 (hwc
->sample_period
== 1)) {
478 /* BTS is not supported by this architecture. */
482 /* BTS is currently only allowed for user-mode. */
483 if (!attr
->exclude_kernel
)
487 hwc
->config
|= config
;
492 static int x86_pmu_hw_config(struct perf_event
*event
)
494 if (event
->attr
.precise_ip
) {
497 /* Support for constant skid */
501 /* Support for IP fixup */
505 if (event
->attr
.precise_ip
> precise
)
511 * (keep 'enabled' bit clear for now)
513 event
->hw
.config
= ARCH_PERFMON_EVENTSEL_INT
;
516 * Count user and OS events unless requested not to
518 if (!event
->attr
.exclude_user
)
519 event
->hw
.config
|= ARCH_PERFMON_EVENTSEL_USR
;
520 if (!event
->attr
.exclude_kernel
)
521 event
->hw
.config
|= ARCH_PERFMON_EVENTSEL_OS
;
523 if (event
->attr
.type
== PERF_TYPE_RAW
)
524 event
->hw
.config
|= event
->attr
.config
& X86_RAW_EVENT_MASK
;
526 return x86_setup_perfctr(event
);
530 * Setup the hardware configuration for a given attr_type
532 static int __hw_perf_event_init(struct perf_event
*event
)
536 if (!x86_pmu_initialized())
540 if (!atomic_inc_not_zero(&active_events
)) {
541 mutex_lock(&pmc_reserve_mutex
);
542 if (atomic_read(&active_events
) == 0) {
543 if (!reserve_pmc_hardware())
546 err
= reserve_ds_buffers();
548 release_pmc_hardware();
552 atomic_inc(&active_events
);
553 mutex_unlock(&pmc_reserve_mutex
);
558 event
->destroy
= hw_perf_event_destroy
;
561 event
->hw
.last_cpu
= -1;
562 event
->hw
.last_tag
= ~0ULL;
564 return x86_pmu
.hw_config(event
);
567 static void x86_pmu_disable_all(void)
569 struct cpu_hw_events
*cpuc
= &__get_cpu_var(cpu_hw_events
);
572 for (idx
= 0; idx
< x86_pmu
.num_counters
; idx
++) {
575 if (!test_bit(idx
, cpuc
->active_mask
))
577 rdmsrl(x86_pmu
.eventsel
+ idx
, val
);
578 if (!(val
& ARCH_PERFMON_EVENTSEL_ENABLE
))
580 val
&= ~ARCH_PERFMON_EVENTSEL_ENABLE
;
581 wrmsrl(x86_pmu
.eventsel
+ idx
, val
);
585 void hw_perf_disable(void)
587 struct cpu_hw_events
*cpuc
= &__get_cpu_var(cpu_hw_events
);
589 if (!x86_pmu_initialized())
599 x86_pmu
.disable_all();
602 static void x86_pmu_enable_all(int added
)
604 struct cpu_hw_events
*cpuc
= &__get_cpu_var(cpu_hw_events
);
607 for (idx
= 0; idx
< x86_pmu
.num_counters
; idx
++) {
608 struct perf_event
*event
= cpuc
->events
[idx
];
611 if (!test_bit(idx
, cpuc
->active_mask
))
614 val
= event
->hw
.config
;
615 val
|= ARCH_PERFMON_EVENTSEL_ENABLE
;
616 wrmsrl(x86_pmu
.eventsel
+ idx
, val
);
620 static const struct pmu pmu
;
622 static inline int is_x86_event(struct perf_event
*event
)
624 return event
->pmu
== &pmu
;
627 static int x86_schedule_events(struct cpu_hw_events
*cpuc
, int n
, int *assign
)
629 struct event_constraint
*c
, *constraints
[X86_PMC_IDX_MAX
];
630 unsigned long used_mask
[BITS_TO_LONGS(X86_PMC_IDX_MAX
)];
631 int i
, j
, w
, wmax
, num
= 0;
632 struct hw_perf_event
*hwc
;
634 bitmap_zero(used_mask
, X86_PMC_IDX_MAX
);
636 for (i
= 0; i
< n
; i
++) {
637 c
= x86_pmu
.get_event_constraints(cpuc
, cpuc
->event_list
[i
]);
642 * fastpath, try to reuse previous register
644 for (i
= 0; i
< n
; i
++) {
645 hwc
= &cpuc
->event_list
[i
]->hw
;
652 /* constraint still honored */
653 if (!test_bit(hwc
->idx
, c
->idxmsk
))
656 /* not already used */
657 if (test_bit(hwc
->idx
, used_mask
))
660 __set_bit(hwc
->idx
, used_mask
);
662 assign
[i
] = hwc
->idx
;
671 bitmap_zero(used_mask
, X86_PMC_IDX_MAX
);
674 * weight = number of possible counters
676 * 1 = most constrained, only works on one counter
677 * wmax = least constrained, works on any counter
679 * assign events to counters starting with most
680 * constrained events.
682 wmax
= x86_pmu
.num_counters
;
685 * when fixed event counters are present,
686 * wmax is incremented by 1 to account
687 * for one more choice
689 if (x86_pmu
.num_counters_fixed
)
692 for (w
= 1, num
= n
; num
&& w
<= wmax
; w
++) {
694 for (i
= 0; num
&& i
< n
; i
++) {
696 hwc
= &cpuc
->event_list
[i
]->hw
;
701 for_each_set_bit(j
, c
->idxmsk
, X86_PMC_IDX_MAX
) {
702 if (!test_bit(j
, used_mask
))
706 if (j
== X86_PMC_IDX_MAX
)
709 __set_bit(j
, used_mask
);
718 * scheduling failed or is just a simulation,
719 * free resources if necessary
721 if (!assign
|| num
) {
722 for (i
= 0; i
< n
; i
++) {
723 if (x86_pmu
.put_event_constraints
)
724 x86_pmu
.put_event_constraints(cpuc
, cpuc
->event_list
[i
]);
727 return num
? -ENOSPC
: 0;
731 * dogrp: true if must collect siblings events (group)
732 * returns total number of events and error code
734 static int collect_events(struct cpu_hw_events
*cpuc
, struct perf_event
*leader
, bool dogrp
)
736 struct perf_event
*event
;
739 max_count
= x86_pmu
.num_counters
+ x86_pmu
.num_counters_fixed
;
741 /* current number of events already accepted */
744 if (is_x86_event(leader
)) {
747 cpuc
->event_list
[n
] = leader
;
753 list_for_each_entry(event
, &leader
->sibling_list
, group_entry
) {
754 if (!is_x86_event(event
) ||
755 event
->state
<= PERF_EVENT_STATE_OFF
)
761 cpuc
->event_list
[n
] = event
;
767 static inline void x86_assign_hw_event(struct perf_event
*event
,
768 struct cpu_hw_events
*cpuc
, int i
)
770 struct hw_perf_event
*hwc
= &event
->hw
;
772 hwc
->idx
= cpuc
->assign
[i
];
773 hwc
->last_cpu
= smp_processor_id();
774 hwc
->last_tag
= ++cpuc
->tags
[i
];
776 if (hwc
->idx
== X86_PMC_IDX_FIXED_BTS
) {
777 hwc
->config_base
= 0;
779 } else if (hwc
->idx
>= X86_PMC_IDX_FIXED
) {
780 hwc
->config_base
= MSR_ARCH_PERFMON_FIXED_CTR_CTRL
;
782 * We set it so that event_base + idx in wrmsr/rdmsr maps to
783 * MSR_ARCH_PERFMON_FIXED_CTR0 ... CTR2:
786 MSR_ARCH_PERFMON_FIXED_CTR0
- X86_PMC_IDX_FIXED
;
788 hwc
->config_base
= x86_pmu
.eventsel
;
789 hwc
->event_base
= x86_pmu
.perfctr
;
793 static inline int match_prev_assignment(struct hw_perf_event
*hwc
,
794 struct cpu_hw_events
*cpuc
,
797 return hwc
->idx
== cpuc
->assign
[i
] &&
798 hwc
->last_cpu
== smp_processor_id() &&
799 hwc
->last_tag
== cpuc
->tags
[i
];
802 static int x86_pmu_start(struct perf_event
*event
);
803 static void x86_pmu_stop(struct perf_event
*event
);
805 void hw_perf_enable(void)
807 struct cpu_hw_events
*cpuc
= &__get_cpu_var(cpu_hw_events
);
808 struct perf_event
*event
;
809 struct hw_perf_event
*hwc
;
810 int i
, added
= cpuc
->n_added
;
812 if (!x86_pmu_initialized())
819 int n_running
= cpuc
->n_events
- cpuc
->n_added
;
821 * apply assignment obtained either from
822 * hw_perf_group_sched_in() or x86_pmu_enable()
824 * step1: save events moving to new counters
825 * step2: reprogram moved events into new counters
827 for (i
= 0; i
< n_running
; i
++) {
828 event
= cpuc
->event_list
[i
];
832 * we can avoid reprogramming counter if:
833 * - assigned same counter as last time
834 * - running on same CPU as last time
835 * - no other event has used the counter since
837 if (hwc
->idx
== -1 ||
838 match_prev_assignment(hwc
, cpuc
, i
))
844 for (i
= 0; i
< cpuc
->n_events
; i
++) {
845 event
= cpuc
->event_list
[i
];
848 if (!match_prev_assignment(hwc
, cpuc
, i
))
849 x86_assign_hw_event(event
, cpuc
, i
);
850 else if (i
< n_running
)
853 x86_pmu_start(event
);
856 perf_events_lapic_init();
862 x86_pmu
.enable_all(added
);
865 static inline void __x86_pmu_enable_event(struct hw_perf_event
*hwc
,
868 wrmsrl(hwc
->config_base
+ hwc
->idx
, hwc
->config
| enable_mask
);
871 static inline void x86_pmu_disable_event(struct perf_event
*event
)
873 struct hw_perf_event
*hwc
= &event
->hw
;
875 wrmsrl(hwc
->config_base
+ hwc
->idx
, hwc
->config
);
878 static DEFINE_PER_CPU(u64
[X86_PMC_IDX_MAX
], pmc_prev_left
);
881 * Set the next IRQ period, based on the hwc->period_left value.
882 * To be called with the event disabled in hw:
885 x86_perf_event_set_period(struct perf_event
*event
)
887 struct hw_perf_event
*hwc
= &event
->hw
;
888 s64 left
= atomic64_read(&hwc
->period_left
);
889 s64 period
= hwc
->sample_period
;
890 int ret
= 0, idx
= hwc
->idx
;
892 if (idx
== X86_PMC_IDX_FIXED_BTS
)
896 * If we are way outside a reasonable range then just skip forward:
898 if (unlikely(left
<= -period
)) {
900 atomic64_set(&hwc
->period_left
, left
);
901 hwc
->last_period
= period
;
905 if (unlikely(left
<= 0)) {
907 atomic64_set(&hwc
->period_left
, left
);
908 hwc
->last_period
= period
;
912 * Quirk: certain CPUs dont like it if just 1 hw_event is left:
914 if (unlikely(left
< 2))
917 if (left
> x86_pmu
.max_period
)
918 left
= x86_pmu
.max_period
;
920 per_cpu(pmc_prev_left
[idx
], smp_processor_id()) = left
;
923 * The hw event starts counting from this event offset,
924 * mark it to be able to extra future deltas:
926 atomic64_set(&hwc
->prev_count
, (u64
)-left
);
928 wrmsrl(hwc
->event_base
+ idx
,
929 (u64
)(-left
) & x86_pmu
.cntval_mask
);
931 perf_event_update_userpage(event
);
936 static void x86_pmu_enable_event(struct perf_event
*event
)
938 struct cpu_hw_events
*cpuc
= &__get_cpu_var(cpu_hw_events
);
940 __x86_pmu_enable_event(&event
->hw
,
941 ARCH_PERFMON_EVENTSEL_ENABLE
);
945 * activate a single event
947 * The event is added to the group of enabled events
948 * but only if it can be scehduled with existing events.
950 * Called with PMU disabled. If successful and return value 1,
951 * then guaranteed to call perf_enable() and hw_perf_enable()
953 static int x86_pmu_enable(struct perf_event
*event
)
955 struct cpu_hw_events
*cpuc
= &__get_cpu_var(cpu_hw_events
);
956 struct hw_perf_event
*hwc
;
957 int assign
[X86_PMC_IDX_MAX
];
963 n
= collect_events(cpuc
, event
, false);
968 * If group events scheduling transaction was started,
969 * skip the schedulability test here, it will be peformed
970 * at commit time(->commit_txn) as a whole
972 if (cpuc
->group_flag
& PERF_EVENT_TXN_STARTED
)
975 ret
= x86_pmu
.schedule_events(cpuc
, n
, assign
);
979 * copy new assignment, now we know it is possible
980 * will be used by hw_perf_enable()
982 memcpy(cpuc
->assign
, assign
, n
*sizeof(int));
986 cpuc
->n_added
+= n
- n0
;
987 cpuc
->n_txn
+= n
- n0
;
992 static int x86_pmu_start(struct perf_event
*event
)
994 struct cpu_hw_events
*cpuc
= &__get_cpu_var(cpu_hw_events
);
995 int idx
= event
->hw
.idx
;
1000 x86_perf_event_set_period(event
);
1001 cpuc
->events
[idx
] = event
;
1002 __set_bit(idx
, cpuc
->active_mask
);
1003 x86_pmu
.enable(event
);
1004 perf_event_update_userpage(event
);
1009 static void x86_pmu_unthrottle(struct perf_event
*event
)
1011 int ret
= x86_pmu_start(event
);
1015 void perf_event_print_debug(void)
1017 u64 ctrl
, status
, overflow
, pmc_ctrl
, pmc_count
, prev_left
, fixed
;
1019 struct cpu_hw_events
*cpuc
;
1020 unsigned long flags
;
1023 if (!x86_pmu
.num_counters
)
1026 local_irq_save(flags
);
1028 cpu
= smp_processor_id();
1029 cpuc
= &per_cpu(cpu_hw_events
, cpu
);
1031 if (x86_pmu
.version
>= 2) {
1032 rdmsrl(MSR_CORE_PERF_GLOBAL_CTRL
, ctrl
);
1033 rdmsrl(MSR_CORE_PERF_GLOBAL_STATUS
, status
);
1034 rdmsrl(MSR_CORE_PERF_GLOBAL_OVF_CTRL
, overflow
);
1035 rdmsrl(MSR_ARCH_PERFMON_FIXED_CTR_CTRL
, fixed
);
1036 rdmsrl(MSR_IA32_PEBS_ENABLE
, pebs
);
1039 pr_info("CPU#%d: ctrl: %016llx\n", cpu
, ctrl
);
1040 pr_info("CPU#%d: status: %016llx\n", cpu
, status
);
1041 pr_info("CPU#%d: overflow: %016llx\n", cpu
, overflow
);
1042 pr_info("CPU#%d: fixed: %016llx\n", cpu
, fixed
);
1043 pr_info("CPU#%d: pebs: %016llx\n", cpu
, pebs
);
1045 pr_info("CPU#%d: active: %016llx\n", cpu
, *(u64
*)cpuc
->active_mask
);
1047 for (idx
= 0; idx
< x86_pmu
.num_counters
; idx
++) {
1048 rdmsrl(x86_pmu
.eventsel
+ idx
, pmc_ctrl
);
1049 rdmsrl(x86_pmu
.perfctr
+ idx
, pmc_count
);
1051 prev_left
= per_cpu(pmc_prev_left
[idx
], cpu
);
1053 pr_info("CPU#%d: gen-PMC%d ctrl: %016llx\n",
1054 cpu
, idx
, pmc_ctrl
);
1055 pr_info("CPU#%d: gen-PMC%d count: %016llx\n",
1056 cpu
, idx
, pmc_count
);
1057 pr_info("CPU#%d: gen-PMC%d left: %016llx\n",
1058 cpu
, idx
, prev_left
);
1060 for (idx
= 0; idx
< x86_pmu
.num_counters_fixed
; idx
++) {
1061 rdmsrl(MSR_ARCH_PERFMON_FIXED_CTR0
+ idx
, pmc_count
);
1063 pr_info("CPU#%d: fixed-PMC%d count: %016llx\n",
1064 cpu
, idx
, pmc_count
);
1066 local_irq_restore(flags
);
1069 static void x86_pmu_stop(struct perf_event
*event
)
1071 struct cpu_hw_events
*cpuc
= &__get_cpu_var(cpu_hw_events
);
1072 struct hw_perf_event
*hwc
= &event
->hw
;
1075 if (!__test_and_clear_bit(idx
, cpuc
->active_mask
))
1078 x86_pmu
.disable(event
);
1081 * Drain the remaining delta count out of a event
1082 * that we are disabling:
1084 x86_perf_event_update(event
);
1086 cpuc
->events
[idx
] = NULL
;
1089 static void x86_pmu_disable(struct perf_event
*event
)
1091 struct cpu_hw_events
*cpuc
= &__get_cpu_var(cpu_hw_events
);
1095 * If we're called during a txn, we don't need to do anything.
1096 * The events never got scheduled and ->cancel_txn will truncate
1099 if (cpuc
->group_flag
& PERF_EVENT_TXN_STARTED
)
1102 x86_pmu_stop(event
);
1104 for (i
= 0; i
< cpuc
->n_events
; i
++) {
1105 if (event
== cpuc
->event_list
[i
]) {
1107 if (x86_pmu
.put_event_constraints
)
1108 x86_pmu
.put_event_constraints(cpuc
, event
);
1110 while (++i
< cpuc
->n_events
)
1111 cpuc
->event_list
[i
-1] = cpuc
->event_list
[i
];
1117 perf_event_update_userpage(event
);
1120 static int x86_pmu_handle_irq(struct pt_regs
*regs
)
1122 struct perf_sample_data data
;
1123 struct cpu_hw_events
*cpuc
;
1124 struct perf_event
*event
;
1125 struct hw_perf_event
*hwc
;
1126 int idx
, handled
= 0;
1129 perf_sample_data_init(&data
, 0);
1131 cpuc
= &__get_cpu_var(cpu_hw_events
);
1133 for (idx
= 0; idx
< x86_pmu
.num_counters
; idx
++) {
1134 if (!test_bit(idx
, cpuc
->active_mask
))
1137 event
= cpuc
->events
[idx
];
1140 val
= x86_perf_event_update(event
);
1141 if (val
& (1ULL << (x86_pmu
.cntval_bits
- 1)))
1148 data
.period
= event
->hw
.last_period
;
1150 if (!x86_perf_event_set_period(event
))
1153 if (perf_event_overflow(event
, 1, &data
, regs
))
1154 x86_pmu_stop(event
);
1158 inc_irq_stat(apic_perf_irqs
);
1163 void smp_perf_pending_interrupt(struct pt_regs
*regs
)
1167 inc_irq_stat(apic_pending_irqs
);
1168 perf_event_do_pending();
1172 void set_perf_event_pending(void)
1174 #ifdef CONFIG_X86_LOCAL_APIC
1175 if (!x86_pmu
.apic
|| !x86_pmu_initialized())
1178 apic
->send_IPI_self(LOCAL_PENDING_VECTOR
);
1182 void perf_events_lapic_init(void)
1184 if (!x86_pmu
.apic
|| !x86_pmu_initialized())
1188 * Always use NMI for PMU
1190 apic_write(APIC_LVTPC
, APIC_DM_NMI
);
1193 static int __kprobes
1194 perf_event_nmi_handler(struct notifier_block
*self
,
1195 unsigned long cmd
, void *__args
)
1197 struct die_args
*args
= __args
;
1198 struct pt_regs
*regs
;
1200 if (!atomic_read(&active_events
))
1214 apic_write(APIC_LVTPC
, APIC_DM_NMI
);
1216 * Can't rely on the handled return value to say it was our NMI, two
1217 * events could trigger 'simultaneously' raising two back-to-back NMIs.
1219 * If the first NMI handles both, the latter will be empty and daze
1222 x86_pmu
.handle_irq(regs
);
1227 static __read_mostly
struct notifier_block perf_event_nmi_notifier
= {
1228 .notifier_call
= perf_event_nmi_handler
,
1233 static struct event_constraint unconstrained
;
1234 static struct event_constraint emptyconstraint
;
1236 static struct event_constraint
*
1237 x86_get_event_constraints(struct cpu_hw_events
*cpuc
, struct perf_event
*event
)
1239 struct event_constraint
*c
;
1241 if (x86_pmu
.event_constraints
) {
1242 for_each_event_constraint(c
, x86_pmu
.event_constraints
) {
1243 if ((event
->hw
.config
& c
->cmask
) == c
->code
)
1248 return &unconstrained
;
1251 #include "perf_event_amd.c"
1252 #include "perf_event_p6.c"
1253 #include "perf_event_p4.c"
1254 #include "perf_event_intel_lbr.c"
1255 #include "perf_event_intel_ds.c"
1256 #include "perf_event_intel.c"
1258 static int __cpuinit
1259 x86_pmu_notifier(struct notifier_block
*self
, unsigned long action
, void *hcpu
)
1261 unsigned int cpu
= (long)hcpu
;
1262 int ret
= NOTIFY_OK
;
1264 switch (action
& ~CPU_TASKS_FROZEN
) {
1265 case CPU_UP_PREPARE
:
1266 if (x86_pmu
.cpu_prepare
)
1267 ret
= x86_pmu
.cpu_prepare(cpu
);
1271 if (x86_pmu
.cpu_starting
)
1272 x86_pmu
.cpu_starting(cpu
);
1276 if (x86_pmu
.cpu_dying
)
1277 x86_pmu
.cpu_dying(cpu
);
1280 case CPU_UP_CANCELED
:
1282 if (x86_pmu
.cpu_dead
)
1283 x86_pmu
.cpu_dead(cpu
);
1293 static void __init
pmu_check_apic(void)
1299 pr_info("no APIC, boot with the \"lapic\" boot parameter to force-enable it.\n");
1300 pr_info("no hardware sampling interrupt available.\n");
1303 void __init
init_hw_perf_events(void)
1305 struct event_constraint
*c
;
1308 pr_info("Performance Events: ");
1310 switch (boot_cpu_data
.x86_vendor
) {
1311 case X86_VENDOR_INTEL
:
1312 err
= intel_pmu_init();
1314 case X86_VENDOR_AMD
:
1315 err
= amd_pmu_init();
1321 pr_cont("no PMU driver, software events only.\n");
1327 pr_cont("%s PMU driver.\n", x86_pmu
.name
);
1332 if (x86_pmu
.num_counters
> X86_PMC_MAX_GENERIC
) {
1333 WARN(1, KERN_ERR
"hw perf events %d > max(%d), clipping!",
1334 x86_pmu
.num_counters
, X86_PMC_MAX_GENERIC
);
1335 x86_pmu
.num_counters
= X86_PMC_MAX_GENERIC
;
1337 x86_pmu
.intel_ctrl
= (1 << x86_pmu
.num_counters
) - 1;
1338 perf_max_events
= x86_pmu
.num_counters
;
1340 if (x86_pmu
.num_counters_fixed
> X86_PMC_MAX_FIXED
) {
1341 WARN(1, KERN_ERR
"hw perf events fixed %d > max(%d), clipping!",
1342 x86_pmu
.num_counters_fixed
, X86_PMC_MAX_FIXED
);
1343 x86_pmu
.num_counters_fixed
= X86_PMC_MAX_FIXED
;
1346 x86_pmu
.intel_ctrl
|=
1347 ((1LL << x86_pmu
.num_counters_fixed
)-1) << X86_PMC_IDX_FIXED
;
1349 perf_events_lapic_init();
1350 register_die_notifier(&perf_event_nmi_notifier
);
1352 unconstrained
= (struct event_constraint
)
1353 __EVENT_CONSTRAINT(0, (1ULL << x86_pmu
.num_counters
) - 1,
1354 0, x86_pmu
.num_counters
);
1356 if (x86_pmu
.event_constraints
) {
1357 for_each_event_constraint(c
, x86_pmu
.event_constraints
) {
1358 if (c
->cmask
!= X86_RAW_EVENT_MASK
)
1361 c
->idxmsk64
|= (1ULL << x86_pmu
.num_counters
) - 1;
1362 c
->weight
+= x86_pmu
.num_counters
;
1366 pr_info("... version: %d\n", x86_pmu
.version
);
1367 pr_info("... bit width: %d\n", x86_pmu
.cntval_bits
);
1368 pr_info("... generic registers: %d\n", x86_pmu
.num_counters
);
1369 pr_info("... value mask: %016Lx\n", x86_pmu
.cntval_mask
);
1370 pr_info("... max period: %016Lx\n", x86_pmu
.max_period
);
1371 pr_info("... fixed-purpose events: %d\n", x86_pmu
.num_counters_fixed
);
1372 pr_info("... event mask: %016Lx\n", x86_pmu
.intel_ctrl
);
1374 perf_cpu_notifier(x86_pmu_notifier
);
1377 static inline void x86_pmu_read(struct perf_event
*event
)
1379 x86_perf_event_update(event
);
1383 * Start group events scheduling transaction
1384 * Set the flag to make pmu::enable() not perform the
1385 * schedulability test, it will be performed at commit time
1387 static void x86_pmu_start_txn(const struct pmu
*pmu
)
1389 struct cpu_hw_events
*cpuc
= &__get_cpu_var(cpu_hw_events
);
1391 cpuc
->group_flag
|= PERF_EVENT_TXN_STARTED
;
1396 * Stop group events scheduling transaction
1397 * Clear the flag and pmu::enable() will perform the
1398 * schedulability test.
1400 static void x86_pmu_cancel_txn(const struct pmu
*pmu
)
1402 struct cpu_hw_events
*cpuc
= &__get_cpu_var(cpu_hw_events
);
1404 cpuc
->group_flag
&= ~PERF_EVENT_TXN_STARTED
;
1406 * Truncate the collected events.
1408 cpuc
->n_added
-= cpuc
->n_txn
;
1409 cpuc
->n_events
-= cpuc
->n_txn
;
1413 * Commit group events scheduling transaction
1414 * Perform the group schedulability test as a whole
1415 * Return 0 if success
1417 static int x86_pmu_commit_txn(const struct pmu
*pmu
)
1419 struct cpu_hw_events
*cpuc
= &__get_cpu_var(cpu_hw_events
);
1420 int assign
[X86_PMC_IDX_MAX
];
1425 if (!x86_pmu_initialized())
1428 ret
= x86_pmu
.schedule_events(cpuc
, n
, assign
);
1433 * copy new assignment, now we know it is possible
1434 * will be used by hw_perf_enable()
1436 memcpy(cpuc
->assign
, assign
, n
*sizeof(int));
1439 * Clear out the txn count so that ->cancel_txn() which gets
1440 * run after ->commit_txn() doesn't undo things.
1447 static const struct pmu pmu
= {
1448 .enable
= x86_pmu_enable
,
1449 .disable
= x86_pmu_disable
,
1450 .start
= x86_pmu_start
,
1451 .stop
= x86_pmu_stop
,
1452 .read
= x86_pmu_read
,
1453 .unthrottle
= x86_pmu_unthrottle
,
1454 .start_txn
= x86_pmu_start_txn
,
1455 .cancel_txn
= x86_pmu_cancel_txn
,
1456 .commit_txn
= x86_pmu_commit_txn
,
1460 * validate that we can schedule this event
1462 static int validate_event(struct perf_event
*event
)
1464 struct cpu_hw_events
*fake_cpuc
;
1465 struct event_constraint
*c
;
1468 fake_cpuc
= kmalloc(sizeof(*fake_cpuc
), GFP_KERNEL
| __GFP_ZERO
);
1472 c
= x86_pmu
.get_event_constraints(fake_cpuc
, event
);
1474 if (!c
|| !c
->weight
)
1477 if (x86_pmu
.put_event_constraints
)
1478 x86_pmu
.put_event_constraints(fake_cpuc
, event
);
1486 * validate a single event group
1488 * validation include:
1489 * - check events are compatible which each other
1490 * - events do not compete for the same counter
1491 * - number of events <= number of counters
1493 * validation ensures the group can be loaded onto the
1494 * PMU if it was the only group available.
1496 static int validate_group(struct perf_event
*event
)
1498 struct perf_event
*leader
= event
->group_leader
;
1499 struct cpu_hw_events
*fake_cpuc
;
1503 fake_cpuc
= kmalloc(sizeof(*fake_cpuc
), GFP_KERNEL
| __GFP_ZERO
);
1508 * the event is not yet connected with its
1509 * siblings therefore we must first collect
1510 * existing siblings, then add the new event
1511 * before we can simulate the scheduling
1514 n
= collect_events(fake_cpuc
, leader
, true);
1518 fake_cpuc
->n_events
= n
;
1519 n
= collect_events(fake_cpuc
, event
, false);
1523 fake_cpuc
->n_events
= n
;
1525 ret
= x86_pmu
.schedule_events(fake_cpuc
, n
, NULL
);
1533 const struct pmu
*hw_perf_event_init(struct perf_event
*event
)
1535 const struct pmu
*tmp
;
1538 err
= __hw_perf_event_init(event
);
1541 * we temporarily connect event to its pmu
1542 * such that validate_group() can classify
1543 * it as an x86 event using is_x86_event()
1548 if (event
->group_leader
!= event
)
1549 err
= validate_group(event
);
1551 err
= validate_event(event
);
1557 event
->destroy(event
);
1558 return ERR_PTR(err
);
1569 void callchain_store(struct perf_callchain_entry
*entry
, u64 ip
)
1571 if (entry
->nr
< PERF_MAX_STACK_DEPTH
)
1572 entry
->ip
[entry
->nr
++] = ip
;
1575 static DEFINE_PER_CPU(struct perf_callchain_entry
, pmc_irq_entry
);
1576 static DEFINE_PER_CPU(struct perf_callchain_entry
, pmc_nmi_entry
);
1580 backtrace_warning_symbol(void *data
, char *msg
, unsigned long symbol
)
1582 /* Ignore warnings */
1585 static void backtrace_warning(void *data
, char *msg
)
1587 /* Ignore warnings */
1590 static int backtrace_stack(void *data
, char *name
)
1595 static void backtrace_address(void *data
, unsigned long addr
, int reliable
)
1597 struct perf_callchain_entry
*entry
= data
;
1599 callchain_store(entry
, addr
);
1602 static const struct stacktrace_ops backtrace_ops
= {
1603 .warning
= backtrace_warning
,
1604 .warning_symbol
= backtrace_warning_symbol
,
1605 .stack
= backtrace_stack
,
1606 .address
= backtrace_address
,
1607 .walk_stack
= print_context_stack_bp
,
1610 #include "../dumpstack.h"
1613 perf_callchain_kernel(struct pt_regs
*regs
, struct perf_callchain_entry
*entry
)
1615 callchain_store(entry
, PERF_CONTEXT_KERNEL
);
1616 callchain_store(entry
, regs
->ip
);
1618 dump_trace(NULL
, regs
, NULL
, regs
->bp
, &backtrace_ops
, entry
);
1621 #ifdef CONFIG_COMPAT
1623 perf_callchain_user32(struct pt_regs
*regs
, struct perf_callchain_entry
*entry
)
1625 /* 32-bit process in 64-bit kernel. */
1626 struct stack_frame_ia32 frame
;
1627 const void __user
*fp
;
1629 if (!test_thread_flag(TIF_IA32
))
1632 fp
= compat_ptr(regs
->bp
);
1633 while (entry
->nr
< PERF_MAX_STACK_DEPTH
) {
1634 unsigned long bytes
;
1635 frame
.next_frame
= 0;
1636 frame
.return_address
= 0;
1638 bytes
= copy_from_user_nmi(&frame
, fp
, sizeof(frame
));
1639 if (bytes
!= sizeof(frame
))
1642 if (fp
< compat_ptr(regs
->sp
))
1645 callchain_store(entry
, frame
.return_address
);
1646 fp
= compat_ptr(frame
.next_frame
);
1652 perf_callchain_user32(struct pt_regs
*regs
, struct perf_callchain_entry
*entry
)
1659 perf_callchain_user(struct pt_regs
*regs
, struct perf_callchain_entry
*entry
)
1661 struct stack_frame frame
;
1662 const void __user
*fp
;
1664 if (!user_mode(regs
))
1665 regs
= task_pt_regs(current
);
1667 fp
= (void __user
*)regs
->bp
;
1669 callchain_store(entry
, PERF_CONTEXT_USER
);
1670 callchain_store(entry
, regs
->ip
);
1672 if (perf_callchain_user32(regs
, entry
))
1675 while (entry
->nr
< PERF_MAX_STACK_DEPTH
) {
1676 unsigned long bytes
;
1677 frame
.next_frame
= NULL
;
1678 frame
.return_address
= 0;
1680 bytes
= copy_from_user_nmi(&frame
, fp
, sizeof(frame
));
1681 if (bytes
!= sizeof(frame
))
1684 if ((unsigned long)fp
< regs
->sp
)
1687 callchain_store(entry
, frame
.return_address
);
1688 fp
= frame
.next_frame
;
1693 perf_do_callchain(struct pt_regs
*regs
, struct perf_callchain_entry
*entry
)
1700 is_user
= user_mode(regs
);
1702 if (is_user
&& current
->state
!= TASK_RUNNING
)
1706 perf_callchain_kernel(regs
, entry
);
1709 perf_callchain_user(regs
, entry
);
1712 struct perf_callchain_entry
*perf_callchain(struct pt_regs
*regs
)
1714 struct perf_callchain_entry
*entry
;
1716 if (perf_guest_cbs
&& perf_guest_cbs
->is_in_guest()) {
1717 /* TODO: We don't support guest os callchain now */
1722 entry
= &__get_cpu_var(pmc_nmi_entry
);
1724 entry
= &__get_cpu_var(pmc_irq_entry
);
1728 perf_do_callchain(regs
, entry
);
1733 void perf_arch_fetch_caller_regs(struct pt_regs
*regs
, unsigned long ip
, int skip
)
1737 * perf_arch_fetch_caller_regs adds another call, we need to increment
1740 regs
->bp
= rewind_frame_pointer(skip
+ 1);
1741 regs
->cs
= __KERNEL_CS
;
1743 * We abuse bit 3 to pass exact information, see perf_misc_flags
1744 * and the comment with PERF_EFLAGS_EXACT.
1749 unsigned long perf_instruction_pointer(struct pt_regs
*regs
)
1753 if (perf_guest_cbs
&& perf_guest_cbs
->is_in_guest())
1754 ip
= perf_guest_cbs
->get_guest_ip();
1756 ip
= instruction_pointer(regs
);
1761 unsigned long perf_misc_flags(struct pt_regs
*regs
)
1765 if (perf_guest_cbs
&& perf_guest_cbs
->is_in_guest()) {
1766 if (perf_guest_cbs
->is_user_mode())
1767 misc
|= PERF_RECORD_MISC_GUEST_USER
;
1769 misc
|= PERF_RECORD_MISC_GUEST_KERNEL
;
1771 if (user_mode(regs
))
1772 misc
|= PERF_RECORD_MISC_USER
;
1774 misc
|= PERF_RECORD_MISC_KERNEL
;
1777 if (regs
->flags
& PERF_EFLAGS_EXACT
)
1778 misc
|= PERF_RECORD_MISC_EXACT_IP
;