4 * Copyright (C) 2008-2009, Thomas Gleixner <tglx@linutronix.de>
5 * Copyright (C) 2008-2011, Red Hat, Inc., Ingo Molnar
6 * Copyright (C) 2008-2011, Red Hat, Inc., Peter Zijlstra
8 * Data type definitions, declarations, prototypes.
10 * Started by: Thomas Gleixner and Ingo Molnar
12 * For licencing details see kernel-base/COPYING
14 #ifndef _LINUX_PERF_EVENT_H
15 #define _LINUX_PERF_EVENT_H
17 #include <uapi/linux/perf_event.h>
20 * Kernel-internal data types and definitions:
23 #ifdef CONFIG_PERF_EVENTS
24 # include <asm/perf_event.h>
25 # include <asm/local64.h>
28 struct perf_guest_info_callbacks {
29 int (*is_in_guest)(void);
30 int (*is_user_mode)(void);
31 unsigned long (*get_guest_ip)(void);
34 #ifdef CONFIG_HAVE_HW_BREAKPOINT
35 #include <asm/hw_breakpoint.h>
38 #include <linux/list.h>
39 #include <linux/mutex.h>
40 #include <linux/rculist.h>
41 #include <linux/rcupdate.h>
42 #include <linux/spinlock.h>
43 #include <linux/hrtimer.h>
45 #include <linux/pid_namespace.h>
46 #include <linux/workqueue.h>
47 #include <linux/ftrace.h>
48 #include <linux/cpu.h>
49 #include <linux/irq_work.h>
50 #include <linux/static_key.h>
51 #include <linux/jump_label_ratelimit.h>
52 #include <linux/atomic.h>
53 #include <linux/sysfs.h>
54 #include <linux/perf_regs.h>
55 #include <linux/workqueue.h>
56 #include <linux/cgroup.h>
57 #include <asm/local.h>
59 struct perf_callchain_entry {
61 __u64 ip[0]; /* /proc/sys/kernel/perf_event_max_stack */
64 struct perf_raw_record {
70 * branch stack layout:
71 * nr: number of taken branches stored in entries[]
73 * Note that nr can vary from sample to sample
74 * branches (to, from) are stored from most recent
75 * to least recent, i.e., entries[0] contains the most
78 struct perf_branch_stack {
80 struct perf_branch_entry entries[0];
86 * extra PMU register associated with an event
88 struct hw_perf_event_extra {
89 u64 config; /* register value */
90 unsigned int reg; /* register address or index */
91 int alloc; /* extra register already allocated */
92 int idx; /* index in shared_regs->regs[] */
96 * struct hw_perf_event - performance event hardware details:
98 struct hw_perf_event {
99 #ifdef CONFIG_PERF_EVENTS
101 struct { /* hardware */
104 unsigned long config_base;
105 unsigned long event_base;
106 int event_base_rdpmc;
111 struct hw_perf_event_extra extra_reg;
112 struct hw_perf_event_extra branch_reg;
114 struct { /* software */
115 struct hrtimer hrtimer;
117 struct { /* tracepoint */
118 /* for tp_event->class */
119 struct list_head tp_list;
121 struct { /* intel_cqm */
125 struct list_head cqm_events_entry;
126 struct list_head cqm_groups_entry;
127 struct list_head cqm_group_entry;
129 struct { /* itrace */
132 struct { /* amd_power */
136 #ifdef CONFIG_HAVE_HW_BREAKPOINT
137 struct { /* breakpoint */
139 * Crufty hack to avoid the chicken and egg
140 * problem hw_breakpoint has with context
141 * creation and event initalization.
143 struct arch_hw_breakpoint info;
144 struct list_head bp_list;
149 * If the event is a per task event, this will point to the task in
150 * question. See the comment in perf_event_alloc().
152 struct task_struct *target;
155 * PMU would store hardware filter configuration
160 /* Last sync'ed generation of filters */
161 unsigned long addr_filters_gen;
164 * hw_perf_event::state flags; used to track the PERF_EF_* state.
166 #define PERF_HES_STOPPED 0x01 /* the counter is stopped */
167 #define PERF_HES_UPTODATE 0x02 /* event->count up-to-date */
168 #define PERF_HES_ARCH 0x04
173 * The last observed hardware counter value, updated with a
174 * local64_cmpxchg() such that pmu::read() can be called nested.
176 local64_t prev_count;
179 * The period to start the next sample with.
184 * The period we started this sample with.
189 * However much is left of the current period; note that this is
190 * a full 64bit value and allows for generation of periods longer
191 * than hardware might allow.
193 local64_t period_left;
196 * State for throttling the event, see __perf_event_overflow() and
197 * perf_adjust_freq_unthr_context().
203 * State for freq target events, see __perf_event_overflow() and
204 * perf_adjust_freq_unthr_context().
207 u64 freq_count_stamp;
214 * Common implementation detail of pmu::{start,commit,cancel}_txn
216 #define PERF_PMU_TXN_ADD 0x1 /* txn to add/schedule event on PMU */
217 #define PERF_PMU_TXN_READ 0x2 /* txn to read event group from PMU */
220 * pmu::capabilities flags
222 #define PERF_PMU_CAP_NO_INTERRUPT 0x01
223 #define PERF_PMU_CAP_NO_NMI 0x02
224 #define PERF_PMU_CAP_AUX_NO_SG 0x04
225 #define PERF_PMU_CAP_AUX_SW_DOUBLEBUF 0x08
226 #define PERF_PMU_CAP_EXCLUSIVE 0x10
227 #define PERF_PMU_CAP_ITRACE 0x20
228 #define PERF_PMU_CAP_HETEROGENEOUS_CPUS 0x40
231 * struct pmu - generic performance monitoring unit
234 struct list_head entry;
236 struct module *module;
238 const struct attribute_group **attr_groups;
243 * various common per-pmu feature flags
247 int * __percpu pmu_disable_count;
248 struct perf_cpu_context * __percpu pmu_cpu_context;
249 atomic_t exclusive_cnt; /* < 0: cpu; > 0: tsk */
251 int hrtimer_interval_ms;
253 /* number of address filters this PMU can do */
254 unsigned int nr_addr_filters;
257 * Fully disable/enable this PMU, can be used to protect from the PMI
258 * as well as for lazy/batch writing of the MSRs.
260 void (*pmu_enable) (struct pmu *pmu); /* optional */
261 void (*pmu_disable) (struct pmu *pmu); /* optional */
264 * Try and initialize the event for this PMU.
267 * -ENOENT -- @event is not for this PMU
269 * -ENODEV -- @event is for this PMU but PMU not present
270 * -EBUSY -- @event is for this PMU but PMU temporarily unavailable
271 * -EINVAL -- @event is for this PMU but @event is not valid
272 * -EOPNOTSUPP -- @event is for this PMU, @event is valid, but not supported
273 * -EACCESS -- @event is for this PMU, @event is valid, but no privilidges
275 * 0 -- @event is for this PMU and valid
277 * Other error return values are allowed.
279 int (*event_init) (struct perf_event *event);
282 * Notification that the event was mapped or unmapped. Called
283 * in the context of the mapping task.
285 void (*event_mapped) (struct perf_event *event); /*optional*/
286 void (*event_unmapped) (struct perf_event *event); /*optional*/
289 * Flags for ->add()/->del()/ ->start()/->stop(). There are
290 * matching hw_perf_event::state flags.
292 #define PERF_EF_START 0x01 /* start the counter when adding */
293 #define PERF_EF_RELOAD 0x02 /* reload the counter when starting */
294 #define PERF_EF_UPDATE 0x04 /* update the counter when stopping */
297 * Adds/Removes a counter to/from the PMU, can be done inside a
298 * transaction, see the ->*_txn() methods.
300 * The add/del callbacks will reserve all hardware resources required
301 * to service the event, this includes any counter constraint
304 * Called with IRQs disabled and the PMU disabled on the CPU the event
307 * ->add() called without PERF_EF_START should result in the same state
308 * as ->add() followed by ->stop().
310 * ->del() must always PERF_EF_UPDATE stop an event. If it calls
311 * ->stop() that must deal with already being stopped without
314 int (*add) (struct perf_event *event, int flags);
315 void (*del) (struct perf_event *event, int flags);
318 * Starts/Stops a counter present on the PMU.
320 * The PMI handler should stop the counter when perf_event_overflow()
321 * returns !0. ->start() will be used to continue.
323 * Also used to change the sample period.
325 * Called with IRQs disabled and the PMU disabled on the CPU the event
326 * is on -- will be called from NMI context with the PMU generates
329 * ->stop() with PERF_EF_UPDATE will read the counter and update
330 * period/count values like ->read() would.
332 * ->start() with PERF_EF_RELOAD will reprogram the the counter
333 * value, must be preceded by a ->stop() with PERF_EF_UPDATE.
335 void (*start) (struct perf_event *event, int flags);
336 void (*stop) (struct perf_event *event, int flags);
339 * Updates the counter value of the event.
341 * For sampling capable PMUs this will also update the software period
342 * hw_perf_event::period_left field.
344 void (*read) (struct perf_event *event);
347 * Group events scheduling is treated as a transaction, add
348 * group events as a whole and perform one schedulability test.
349 * If the test fails, roll back the whole group
351 * Start the transaction, after this ->add() doesn't need to
352 * do schedulability tests.
356 void (*start_txn) (struct pmu *pmu, unsigned int txn_flags);
358 * If ->start_txn() disabled the ->add() schedulability test
359 * then ->commit_txn() is required to perform one. On success
360 * the transaction is closed. On error the transaction is kept
361 * open until ->cancel_txn() is called.
365 int (*commit_txn) (struct pmu *pmu);
367 * Will cancel the transaction, assumes ->del() is called
368 * for each successful ->add() during the transaction.
372 void (*cancel_txn) (struct pmu *pmu);
375 * Will return the value for perf_event_mmap_page::index for this event,
376 * if no implementation is provided it will default to: event->hw.idx + 1.
378 int (*event_idx) (struct perf_event *event); /*optional */
381 * context-switches callback
383 void (*sched_task) (struct perf_event_context *ctx,
386 * PMU specific data size
388 size_t task_ctx_size;
392 * Return the count value for a counter.
394 u64 (*count) (struct perf_event *event); /*optional*/
397 * Set up pmu-private data structures for an AUX area
399 void *(*setup_aux) (int cpu, void **pages,
400 int nr_pages, bool overwrite);
404 * Free pmu-private AUX data structures
406 void (*free_aux) (void *aux); /* optional */
409 * Validate address range filters: make sure the HW supports the
410 * requested configuration and number of filters; return 0 if the
411 * supplied filters are valid, -errno otherwise.
413 * Runs in the context of the ioctl()ing process and is not serialized
414 * with the rest of the PMU callbacks.
416 int (*addr_filters_validate) (struct list_head *filters);
420 * Synchronize address range filter configuration:
421 * translate hw-agnostic filters into hardware configuration in
422 * event::hw::addr_filters.
424 * Runs as a part of filter sync sequence that is done in ->start()
425 * callback by calling perf_event_addr_filters_sync().
427 * May (and should) traverse event::addr_filters::list, for which its
428 * caller provides necessary serialization.
430 void (*addr_filters_sync) (struct perf_event *event);
434 * Filter events for PMU-specific reasons.
436 int (*filter_match) (struct perf_event *event); /* optional */
440 * struct perf_addr_filter - address range filter definition
441 * @entry: event's filter list linkage
442 * @inode: object file's inode for file-based filters
443 * @offset: filter range offset
444 * @size: filter range size
445 * @range: 1: range, 0: address
446 * @filter: 1: filter/start, 0: stop
448 * This is a hardware-agnostic filter configuration as specified by the user.
450 struct perf_addr_filter {
451 struct list_head entry;
453 unsigned long offset;
455 unsigned int range : 1,
460 * struct perf_addr_filters_head - container for address range filters
461 * @list: list of filters for this event
462 * @lock: spinlock that serializes accesses to the @list and event's
463 * (and its children's) filter generations.
465 * A child event will use parent's @list (and therefore @lock), so they are
466 * bundled together; see perf_event_addr_filters().
468 struct perf_addr_filters_head {
469 struct list_head list;
474 * enum perf_event_active_state - the states of a event
476 enum perf_event_active_state {
477 PERF_EVENT_STATE_DEAD = -4,
478 PERF_EVENT_STATE_EXIT = -3,
479 PERF_EVENT_STATE_ERROR = -2,
480 PERF_EVENT_STATE_OFF = -1,
481 PERF_EVENT_STATE_INACTIVE = 0,
482 PERF_EVENT_STATE_ACTIVE = 1,
486 struct perf_sample_data;
488 typedef void (*perf_overflow_handler_t)(struct perf_event *,
489 struct perf_sample_data *,
490 struct pt_regs *regs);
492 enum perf_group_flag {
493 PERF_GROUP_SOFTWARE = 0x1,
496 #define SWEVENT_HLIST_BITS 8
497 #define SWEVENT_HLIST_SIZE (1 << SWEVENT_HLIST_BITS)
499 struct swevent_hlist {
500 struct hlist_head heads[SWEVENT_HLIST_SIZE];
501 struct rcu_head rcu_head;
504 #define PERF_ATTACH_CONTEXT 0x01
505 #define PERF_ATTACH_GROUP 0x02
506 #define PERF_ATTACH_TASK 0x04
507 #define PERF_ATTACH_TASK_DATA 0x08
513 * struct perf_event - performance event kernel representation:
516 #ifdef CONFIG_PERF_EVENTS
518 * entry onto perf_event_context::event_list;
519 * modifications require ctx->lock
520 * RCU safe iterations.
522 struct list_head event_entry;
525 * XXX: group_entry and sibling_list should be mutually exclusive;
526 * either you're a sibling on a group, or you're the group leader.
527 * Rework the code to always use the same list element.
529 * Locked for modification by both ctx->mutex and ctx->lock; holding
530 * either sufficies for read.
532 struct list_head group_entry;
533 struct list_head sibling_list;
536 * We need storage to track the entries in perf_pmu_migrate_context; we
537 * cannot use the event_entry because of RCU and we want to keep the
538 * group in tact which avoids us using the other two entries.
540 struct list_head migrate_entry;
542 struct hlist_node hlist_entry;
543 struct list_head active_entry;
546 struct perf_event *group_leader;
550 enum perf_event_active_state state;
551 unsigned int attach_state;
553 atomic64_t child_count;
556 * These are the total time in nanoseconds that the event
557 * has been enabled (i.e. eligible to run, and the task has
558 * been scheduled in, if this is a per-task event)
559 * and running (scheduled onto the CPU), respectively.
561 * They are computed from tstamp_enabled, tstamp_running and
562 * tstamp_stopped when the event is in INACTIVE or ACTIVE state.
564 u64 total_time_enabled;
565 u64 total_time_running;
568 * These are timestamps used for computing total_time_enabled
569 * and total_time_running when the event is in INACTIVE or
570 * ACTIVE state, measured in nanoseconds from an arbitrary point
572 * tstamp_enabled: the notional time when the event was enabled
573 * tstamp_running: the notional time when the event was scheduled on
574 * tstamp_stopped: in INACTIVE state, the notional time when the
575 * event was scheduled off.
582 * timestamp shadows the actual context timing but it can
583 * be safely used in NMI interrupt context. It reflects the
584 * context time as it was when the event was last scheduled in.
586 * ctx_time already accounts for ctx->timestamp. Therefore to
587 * compute ctx_time for a sample, simply add perf_clock().
591 struct perf_event_attr attr;
595 struct hw_perf_event hw;
597 struct perf_event_context *ctx;
598 atomic_long_t refcount;
601 * These accumulate total time (in nanoseconds) that children
602 * events have been enabled and running, respectively.
604 atomic64_t child_total_time_enabled;
605 atomic64_t child_total_time_running;
608 * Protect attach/detach and child_list:
610 struct mutex child_mutex;
611 struct list_head child_list;
612 struct perf_event *parent;
617 struct list_head owner_entry;
618 struct task_struct *owner;
621 struct mutex mmap_mutex;
624 struct ring_buffer *rb;
625 struct list_head rb_entry;
626 unsigned long rcu_batches;
630 wait_queue_head_t waitq;
631 struct fasync_struct *fasync;
633 /* delayed work for NMIs and such */
637 struct irq_work pending;
639 atomic_t event_limit;
641 /* address range filters */
642 struct perf_addr_filters_head addr_filters;
643 /* vma address array for file-based filders */
644 unsigned long *addr_filters_offs;
645 unsigned long addr_filters_gen;
647 void (*destroy)(struct perf_event *);
648 struct rcu_head rcu_head;
650 struct pid_namespace *ns;
654 perf_overflow_handler_t overflow_handler;
655 void *overflow_handler_context;
657 #ifdef CONFIG_EVENT_TRACING
658 struct trace_event_call *tp_event;
659 struct event_filter *filter;
660 #ifdef CONFIG_FUNCTION_TRACER
661 struct ftrace_ops ftrace_ops;
665 #ifdef CONFIG_CGROUP_PERF
666 struct perf_cgroup *cgrp; /* cgroup event is attach to */
667 int cgrp_defer_enabled;
670 #endif /* CONFIG_PERF_EVENTS */
674 * struct perf_event_context - event context structure
676 * Used as a container for task events and CPU events as well:
678 struct perf_event_context {
681 * Protect the states of the events in the list,
682 * nr_active, and the list:
686 * Protect the list of events. Locking either mutex or lock
687 * is sufficient to ensure the list doesn't change; to change
688 * the list you need to lock both the mutex and the spinlock.
692 struct list_head active_ctx_list;
693 struct list_head pinned_groups;
694 struct list_head flexible_groups;
695 struct list_head event_list;
703 struct task_struct *task;
706 * Context clock, runs when context enabled.
712 * These fields let us detect when two contexts have both
713 * been cloned (inherited) from a common ancestor.
715 struct perf_event_context *parent_ctx;
719 int nr_cgroups; /* cgroup evts */
720 void *task_ctx_data; /* pmu specific data */
721 struct rcu_head rcu_head;
725 * Number of contexts where an event can trigger:
726 * task, softirq, hardirq, nmi.
728 #define PERF_NR_CONTEXTS 4
731 * struct perf_event_cpu_context - per cpu event context structure
733 struct perf_cpu_context {
734 struct perf_event_context ctx;
735 struct perf_event_context *task_ctx;
739 raw_spinlock_t hrtimer_lock;
740 struct hrtimer hrtimer;
741 ktime_t hrtimer_interval;
742 unsigned int hrtimer_active;
744 struct pmu *unique_pmu;
745 struct perf_cgroup *cgrp;
748 struct perf_output_handle {
749 struct perf_event *event;
750 struct ring_buffer *rb;
751 unsigned long wakeup;
760 #ifdef CONFIG_CGROUP_PERF
763 * perf_cgroup_info keeps track of time_enabled for a cgroup.
764 * This is a per-cpu dynamically allocated data structure.
766 struct perf_cgroup_info {
772 struct cgroup_subsys_state css;
773 struct perf_cgroup_info __percpu *info;
777 * Must ensure cgroup is pinned (css_get) before calling
778 * this function. In other words, we cannot call this function
779 * if there is no cgroup event for the current CPU context.
781 static inline struct perf_cgroup *
782 perf_cgroup_from_task(struct task_struct *task, struct perf_event_context *ctx)
784 return container_of(task_css_check(task, perf_event_cgrp_id,
785 ctx ? lockdep_is_held(&ctx->lock)
787 struct perf_cgroup, css);
789 #endif /* CONFIG_CGROUP_PERF */
791 #ifdef CONFIG_PERF_EVENTS
793 extern void *perf_aux_output_begin(struct perf_output_handle *handle,
794 struct perf_event *event);
795 extern void perf_aux_output_end(struct perf_output_handle *handle,
796 unsigned long size, bool truncated);
797 extern int perf_aux_output_skip(struct perf_output_handle *handle,
799 extern void *perf_get_aux(struct perf_output_handle *handle);
801 extern int perf_pmu_register(struct pmu *pmu, const char *name, int type);
802 extern void perf_pmu_unregister(struct pmu *pmu);
804 extern int perf_num_counters(void);
805 extern const char *perf_pmu_name(void);
806 extern void __perf_event_task_sched_in(struct task_struct *prev,
807 struct task_struct *task);
808 extern void __perf_event_task_sched_out(struct task_struct *prev,
809 struct task_struct *next);
810 extern int perf_event_init_task(struct task_struct *child);
811 extern void perf_event_exit_task(struct task_struct *child);
812 extern void perf_event_free_task(struct task_struct *task);
813 extern void perf_event_delayed_put(struct task_struct *task);
814 extern struct file *perf_event_get(unsigned int fd);
815 extern const struct perf_event_attr *perf_event_attrs(struct perf_event *event);
816 extern void perf_event_print_debug(void);
817 extern void perf_pmu_disable(struct pmu *pmu);
818 extern void perf_pmu_enable(struct pmu *pmu);
819 extern void perf_sched_cb_dec(struct pmu *pmu);
820 extern void perf_sched_cb_inc(struct pmu *pmu);
821 extern int perf_event_task_disable(void);
822 extern int perf_event_task_enable(void);
823 extern int perf_event_refresh(struct perf_event *event, int refresh);
824 extern void perf_event_update_userpage(struct perf_event *event);
825 extern int perf_event_release_kernel(struct perf_event *event);
826 extern struct perf_event *
827 perf_event_create_kernel_counter(struct perf_event_attr *attr,
829 struct task_struct *task,
830 perf_overflow_handler_t callback,
832 extern void perf_pmu_migrate_context(struct pmu *pmu,
833 int src_cpu, int dst_cpu);
834 extern u64 perf_event_read_local(struct perf_event *event);
835 extern u64 perf_event_read_value(struct perf_event *event,
836 u64 *enabled, u64 *running);
839 struct perf_sample_data {
841 * Fields set by perf_sample_data_init(), group so as to
842 * minimize the cachelines touched.
845 struct perf_raw_record *raw;
846 struct perf_branch_stack *br_stack;
850 union perf_mem_data_src data_src;
853 * The other fields, optionally {set,used} by
854 * perf_{prepare,output}_sample().
869 struct perf_callchain_entry *callchain;
872 * regs_user may point to task_pt_regs or to regs_user_copy, depending
875 struct perf_regs regs_user;
876 struct pt_regs regs_user_copy;
878 struct perf_regs regs_intr;
880 } ____cacheline_aligned;
882 /* default value for data source */
883 #define PERF_MEM_NA (PERF_MEM_S(OP, NA) |\
884 PERF_MEM_S(LVL, NA) |\
885 PERF_MEM_S(SNOOP, NA) |\
886 PERF_MEM_S(LOCK, NA) |\
889 static inline void perf_sample_data_init(struct perf_sample_data *data,
890 u64 addr, u64 period)
892 /* remaining struct members initialized in perf_prepare_sample() */
895 data->br_stack = NULL;
896 data->period = period;
898 data->data_src.val = PERF_MEM_NA;
902 extern void perf_output_sample(struct perf_output_handle *handle,
903 struct perf_event_header *header,
904 struct perf_sample_data *data,
905 struct perf_event *event);
906 extern void perf_prepare_sample(struct perf_event_header *header,
907 struct perf_sample_data *data,
908 struct perf_event *event,
909 struct pt_regs *regs);
911 extern int perf_event_overflow(struct perf_event *event,
912 struct perf_sample_data *data,
913 struct pt_regs *regs);
915 extern void perf_event_output_forward(struct perf_event *event,
916 struct perf_sample_data *data,
917 struct pt_regs *regs);
918 extern void perf_event_output_backward(struct perf_event *event,
919 struct perf_sample_data *data,
920 struct pt_regs *regs);
921 extern void perf_event_output(struct perf_event *event,
922 struct perf_sample_data *data,
923 struct pt_regs *regs);
926 is_default_overflow_handler(struct perf_event *event)
928 if (likely(event->overflow_handler == perf_event_output_forward))
930 if (unlikely(event->overflow_handler == perf_event_output_backward))
936 perf_event_header__init_id(struct perf_event_header *header,
937 struct perf_sample_data *data,
938 struct perf_event *event);
940 perf_event__output_id_sample(struct perf_event *event,
941 struct perf_output_handle *handle,
942 struct perf_sample_data *sample);
945 perf_log_lost_samples(struct perf_event *event, u64 lost);
947 static inline bool is_sampling_event(struct perf_event *event)
949 return event->attr.sample_period != 0;
953 * Return 1 for a software event, 0 for a hardware event
955 static inline int is_software_event(struct perf_event *event)
957 return event->pmu->task_ctx_nr == perf_sw_context;
960 extern struct static_key perf_swevent_enabled[PERF_COUNT_SW_MAX];
962 extern void ___perf_sw_event(u32, u64, struct pt_regs *, u64);
963 extern void __perf_sw_event(u32, u64, struct pt_regs *, u64);
965 #ifndef perf_arch_fetch_caller_regs
966 static inline void perf_arch_fetch_caller_regs(struct pt_regs *regs, unsigned long ip) { }
970 * Take a snapshot of the regs. Skip ip and frame pointer to
971 * the nth caller. We only need a few of the regs:
972 * - ip for PERF_SAMPLE_IP
973 * - cs for user_mode() tests
974 * - bp for callchains
975 * - eflags, for future purposes, just in case
977 static inline void perf_fetch_caller_regs(struct pt_regs *regs)
979 memset(regs, 0, sizeof(*regs));
981 perf_arch_fetch_caller_regs(regs, CALLER_ADDR0);
984 static __always_inline void
985 perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr)
987 if (static_key_false(&perf_swevent_enabled[event_id]))
988 __perf_sw_event(event_id, nr, regs, addr);
991 DECLARE_PER_CPU(struct pt_regs, __perf_regs[4]);
994 * 'Special' version for the scheduler, it hard assumes no recursion,
995 * which is guaranteed by us not actually scheduling inside other swevents
996 * because those disable preemption.
998 static __always_inline void
999 perf_sw_event_sched(u32 event_id, u64 nr, u64 addr)
1001 if (static_key_false(&perf_swevent_enabled[event_id])) {
1002 struct pt_regs *regs = this_cpu_ptr(&__perf_regs[0]);
1004 perf_fetch_caller_regs(regs);
1005 ___perf_sw_event(event_id, nr, regs, addr);
1009 extern struct static_key_false perf_sched_events;
1011 static __always_inline bool
1012 perf_sw_migrate_enabled(void)
1014 if (static_key_false(&perf_swevent_enabled[PERF_COUNT_SW_CPU_MIGRATIONS]))
1019 static inline void perf_event_task_migrate(struct task_struct *task)
1021 if (perf_sw_migrate_enabled())
1022 task->sched_migrated = 1;
1025 static inline void perf_event_task_sched_in(struct task_struct *prev,
1026 struct task_struct *task)
1028 if (static_branch_unlikely(&perf_sched_events))
1029 __perf_event_task_sched_in(prev, task);
1031 if (perf_sw_migrate_enabled() && task->sched_migrated) {
1032 struct pt_regs *regs = this_cpu_ptr(&__perf_regs[0]);
1034 perf_fetch_caller_regs(regs);
1035 ___perf_sw_event(PERF_COUNT_SW_CPU_MIGRATIONS, 1, regs, 0);
1036 task->sched_migrated = 0;
1040 static inline void perf_event_task_sched_out(struct task_struct *prev,
1041 struct task_struct *next)
1043 perf_sw_event_sched(PERF_COUNT_SW_CONTEXT_SWITCHES, 1, 0);
1045 if (static_branch_unlikely(&perf_sched_events))
1046 __perf_event_task_sched_out(prev, next);
1049 static inline u64 __perf_event_count(struct perf_event *event)
1051 return local64_read(&event->count) + atomic64_read(&event->child_count);
1054 extern void perf_event_mmap(struct vm_area_struct *vma);
1055 extern struct perf_guest_info_callbacks *perf_guest_cbs;
1056 extern int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *callbacks);
1057 extern int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *callbacks);
1059 extern void perf_event_exec(void);
1060 extern void perf_event_comm(struct task_struct *tsk, bool exec);
1061 extern void perf_event_fork(struct task_struct *tsk);
1064 DECLARE_PER_CPU(struct perf_callchain_entry, perf_callchain_entry);
1066 extern void perf_callchain_user(struct perf_callchain_entry *entry, struct pt_regs *regs);
1067 extern void perf_callchain_kernel(struct perf_callchain_entry *entry, struct pt_regs *regs);
1068 extern struct perf_callchain_entry *
1069 get_perf_callchain(struct pt_regs *regs, u32 init_nr, bool kernel, bool user,
1070 bool crosstask, bool add_mark);
1071 extern int get_callchain_buffers(void);
1072 extern void put_callchain_buffers(void);
1074 extern int sysctl_perf_event_max_stack;
1076 static inline int perf_callchain_store(struct perf_callchain_entry *entry, u64 ip)
1078 if (entry->nr < sysctl_perf_event_max_stack) {
1079 entry->ip[entry->nr++] = ip;
1082 return -1; /* no more room, stop walking the stack */
1086 extern int sysctl_perf_event_paranoid;
1087 extern int sysctl_perf_event_mlock;
1088 extern int sysctl_perf_event_sample_rate;
1089 extern int sysctl_perf_cpu_time_max_percent;
1091 extern void perf_sample_event_took(u64 sample_len_ns);
1093 extern int perf_proc_update_handler(struct ctl_table *table, int write,
1094 void __user *buffer, size_t *lenp,
1096 extern int perf_cpu_time_max_percent_handler(struct ctl_table *table, int write,
1097 void __user *buffer, size_t *lenp,
1100 int perf_event_max_stack_handler(struct ctl_table *table, int write,
1101 void __user *buffer, size_t *lenp, loff_t *ppos);
1103 static inline bool perf_paranoid_tracepoint_raw(void)
1105 return sysctl_perf_event_paranoid > -1;
1108 static inline bool perf_paranoid_cpu(void)
1110 return sysctl_perf_event_paranoid > 0;
1113 static inline bool perf_paranoid_kernel(void)
1115 return sysctl_perf_event_paranoid > 1;
1118 extern void perf_event_init(void);
1119 extern void perf_tp_event(u64 addr, u64 count, void *record,
1120 int entry_size, struct pt_regs *regs,
1121 struct hlist_head *head, int rctx,
1122 struct task_struct *task);
1123 extern void perf_bp_event(struct perf_event *event, void *data);
1125 #ifndef perf_misc_flags
1126 # define perf_misc_flags(regs) \
1127 (user_mode(regs) ? PERF_RECORD_MISC_USER : PERF_RECORD_MISC_KERNEL)
1128 # define perf_instruction_pointer(regs) instruction_pointer(regs)
1131 static inline bool has_branch_stack(struct perf_event *event)
1133 return event->attr.sample_type & PERF_SAMPLE_BRANCH_STACK;
1136 static inline bool needs_branch_stack(struct perf_event *event)
1138 return event->attr.branch_sample_type != 0;
1141 static inline bool has_aux(struct perf_event *event)
1143 return event->pmu->setup_aux;
1146 static inline bool is_write_backward(struct perf_event *event)
1148 return !!event->attr.write_backward;
1151 static inline bool has_addr_filter(struct perf_event *event)
1153 return event->pmu->nr_addr_filters;
1157 * An inherited event uses parent's filters
1159 static inline struct perf_addr_filters_head *
1160 perf_event_addr_filters(struct perf_event *event)
1162 struct perf_addr_filters_head *ifh = &event->addr_filters;
1165 ifh = &event->parent->addr_filters;
1170 extern void perf_event_addr_filters_sync(struct perf_event *event);
1172 extern int perf_output_begin(struct perf_output_handle *handle,
1173 struct perf_event *event, unsigned int size);
1174 extern int perf_output_begin_forward(struct perf_output_handle *handle,
1175 struct perf_event *event,
1177 extern int perf_output_begin_backward(struct perf_output_handle *handle,
1178 struct perf_event *event,
1181 extern void perf_output_end(struct perf_output_handle *handle);
1182 extern unsigned int perf_output_copy(struct perf_output_handle *handle,
1183 const void *buf, unsigned int len);
1184 extern unsigned int perf_output_skip(struct perf_output_handle *handle,
1186 extern int perf_swevent_get_recursion_context(void);
1187 extern void perf_swevent_put_recursion_context(int rctx);
1188 extern u64 perf_swevent_set_period(struct perf_event *event);
1189 extern void perf_event_enable(struct perf_event *event);
1190 extern void perf_event_disable(struct perf_event *event);
1191 extern void perf_event_disable_local(struct perf_event *event);
1192 extern void perf_event_task_tick(void);
1193 #else /* !CONFIG_PERF_EVENTS: */
1194 static inline void *
1195 perf_aux_output_begin(struct perf_output_handle *handle,
1196 struct perf_event *event) { return NULL; }
1198 perf_aux_output_end(struct perf_output_handle *handle, unsigned long size,
1201 perf_aux_output_skip(struct perf_output_handle *handle,
1202 unsigned long size) { return -EINVAL; }
1203 static inline void *
1204 perf_get_aux(struct perf_output_handle *handle) { return NULL; }
1206 perf_event_task_migrate(struct task_struct *task) { }
1208 perf_event_task_sched_in(struct task_struct *prev,
1209 struct task_struct *task) { }
1211 perf_event_task_sched_out(struct task_struct *prev,
1212 struct task_struct *next) { }
1213 static inline int perf_event_init_task(struct task_struct *child) { return 0; }
1214 static inline void perf_event_exit_task(struct task_struct *child) { }
1215 static inline void perf_event_free_task(struct task_struct *task) { }
1216 static inline void perf_event_delayed_put(struct task_struct *task) { }
1217 static inline struct file *perf_event_get(unsigned int fd) { return ERR_PTR(-EINVAL); }
1218 static inline const struct perf_event_attr *perf_event_attrs(struct perf_event *event)
1220 return ERR_PTR(-EINVAL);
1222 static inline u64 perf_event_read_local(struct perf_event *event) { return -EINVAL; }
1223 static inline void perf_event_print_debug(void) { }
1224 static inline int perf_event_task_disable(void) { return -EINVAL; }
1225 static inline int perf_event_task_enable(void) { return -EINVAL; }
1226 static inline int perf_event_refresh(struct perf_event *event, int refresh)
1232 perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr) { }
1234 perf_sw_event_sched(u32 event_id, u64 nr, u64 addr) { }
1236 perf_bp_event(struct perf_event *event, void *data) { }
1238 static inline int perf_register_guest_info_callbacks
1239 (struct perf_guest_info_callbacks *callbacks) { return 0; }
1240 static inline int perf_unregister_guest_info_callbacks
1241 (struct perf_guest_info_callbacks *callbacks) { return 0; }
1243 static inline void perf_event_mmap(struct vm_area_struct *vma) { }
1244 static inline void perf_event_exec(void) { }
1245 static inline void perf_event_comm(struct task_struct *tsk, bool exec) { }
1246 static inline void perf_event_fork(struct task_struct *tsk) { }
1247 static inline void perf_event_init(void) { }
1248 static inline int perf_swevent_get_recursion_context(void) { return -1; }
1249 static inline void perf_swevent_put_recursion_context(int rctx) { }
1250 static inline u64 perf_swevent_set_period(struct perf_event *event) { return 0; }
1251 static inline void perf_event_enable(struct perf_event *event) { }
1252 static inline void perf_event_disable(struct perf_event *event) { }
1253 static inline int __perf_event_disable(void *info) { return -1; }
1254 static inline void perf_event_task_tick(void) { }
1255 static inline int perf_event_release_kernel(struct perf_event *event) { return 0; }
1258 #if defined(CONFIG_PERF_EVENTS) && defined(CONFIG_CPU_SUP_INTEL)
1259 extern void perf_restore_debug_store(void);
1261 static inline void perf_restore_debug_store(void) { }
1264 #define perf_output_put(handle, x) perf_output_copy((handle), &(x), sizeof(x))
1267 * This has to have a higher priority than migration_notifier in sched/core.c.
1269 #define perf_cpu_notifier(fn) \
1271 static struct notifier_block fn##_nb = \
1272 { .notifier_call = fn, .priority = CPU_PRI_PERF }; \
1273 unsigned long cpu = smp_processor_id(); \
1274 unsigned long flags; \
1276 cpu_notifier_register_begin(); \
1277 fn(&fn##_nb, (unsigned long)CPU_UP_PREPARE, \
1278 (void *)(unsigned long)cpu); \
1279 local_irq_save(flags); \
1280 fn(&fn##_nb, (unsigned long)CPU_STARTING, \
1281 (void *)(unsigned long)cpu); \
1282 local_irq_restore(flags); \
1283 fn(&fn##_nb, (unsigned long)CPU_ONLINE, \
1284 (void *)(unsigned long)cpu); \
1285 __register_cpu_notifier(&fn##_nb); \
1286 cpu_notifier_register_done(); \
1290 * Bare-bones version of perf_cpu_notifier(), which doesn't invoke the
1291 * callback for already online CPUs.
1293 #define __perf_cpu_notifier(fn) \
1295 static struct notifier_block fn##_nb = \
1296 { .notifier_call = fn, .priority = CPU_PRI_PERF }; \
1298 __register_cpu_notifier(&fn##_nb); \
1301 struct perf_pmu_events_attr {
1302 struct device_attribute attr;
1304 const char *event_str;
1307 ssize_t perf_event_sysfs_show(struct device *dev, struct device_attribute *attr,
1310 #define PMU_EVENT_ATTR(_name, _var, _id, _show) \
1311 static struct perf_pmu_events_attr _var = { \
1312 .attr = __ATTR(_name, 0444, _show, NULL), \
1316 #define PMU_EVENT_ATTR_STRING(_name, _var, _str) \
1317 static struct perf_pmu_events_attr _var = { \
1318 .attr = __ATTR(_name, 0444, perf_event_sysfs_show, NULL), \
1320 .event_str = _str, \
1323 #define PMU_FORMAT_ATTR(_name, _format) \
1325 _name##_show(struct device *dev, \
1326 struct device_attribute *attr, \
1329 BUILD_BUG_ON(sizeof(_format) >= PAGE_SIZE); \
1330 return sprintf(page, _format "\n"); \
1333 static struct device_attribute format_attr_##_name = __ATTR_RO(_name)
1335 #endif /* _LINUX_PERF_EVENT_H */