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>
18 #include <uapi/linux/bpf_perf_event.h>
21 * Kernel-internal data types and definitions:
24 #ifdef CONFIG_PERF_EVENTS
25 # include <asm/perf_event.h>
26 # include <asm/local64.h>
29 #define PERF_GUEST_ACTIVE 0x01
30 #define PERF_GUEST_USER 0x02
32 struct perf_guest_info_callbacks {
33 unsigned int (*state)(void);
34 unsigned long (*get_ip)(void);
35 unsigned int (*handle_intel_pt_intr)(void);
38 #ifdef CONFIG_HAVE_HW_BREAKPOINT
39 #include <linux/rhashtable-types.h>
40 #include <asm/hw_breakpoint.h>
43 #include <linux/list.h>
44 #include <linux/mutex.h>
45 #include <linux/rculist.h>
46 #include <linux/rcupdate.h>
47 #include <linux/spinlock.h>
48 #include <linux/hrtimer.h>
50 #include <linux/pid_namespace.h>
51 #include <linux/workqueue.h>
52 #include <linux/ftrace.h>
53 #include <linux/cpu.h>
54 #include <linux/irq_work.h>
55 #include <linux/static_key.h>
56 #include <linux/jump_label_ratelimit.h>
57 #include <linux/atomic.h>
58 #include <linux/sysfs.h>
59 #include <linux/perf_regs.h>
60 #include <linux/cgroup.h>
61 #include <linux/refcount.h>
62 #include <linux/security.h>
63 #include <linux/static_call.h>
64 #include <linux/lockdep.h>
65 #include <asm/local.h>
67 struct perf_callchain_entry {
69 __u64 ip[]; /* /proc/sys/kernel/perf_event_max_stack */
72 struct perf_callchain_entry_ctx {
73 struct perf_callchain_entry *entry;
80 typedef unsigned long (*perf_copy_f)(void *dst, const void *src,
81 unsigned long off, unsigned long len);
83 struct perf_raw_frag {
85 struct perf_raw_frag *next;
93 struct perf_raw_record {
94 struct perf_raw_frag frag;
99 * branch stack layout:
100 * nr: number of taken branches stored in entries[]
101 * hw_idx: The low level index of raw branch records
102 * for the most recent branch.
103 * -1ULL means invalid/unknown.
105 * Note that nr can vary from sample to sample
106 * branches (to, from) are stored from most recent
107 * to least recent, i.e., entries[0] contains the most
109 * The entries[] is an abstraction of raw branch records,
110 * which may not be stored in age order in HW, e.g. Intel LBR.
111 * The hw_idx is to expose the low level index of raw
112 * branch record for the most recent branch aka entries[0].
113 * The hw_idx index is between -1 (unknown) and max depth,
114 * which can be retrieved in /sys/devices/cpu/caps/branches.
115 * For the architectures whose raw branch records are
116 * already stored in age order, the hw_idx should be 0.
118 struct perf_branch_stack {
121 struct perf_branch_entry entries[];
127 * extra PMU register associated with an event
129 struct hw_perf_event_extra {
130 u64 config; /* register value */
131 unsigned int reg; /* register address or index */
132 int alloc; /* extra register already allocated */
133 int idx; /* index in shared_regs->regs[] */
137 * hw_perf_event::flag values
139 * PERF_EVENT_FLAG_ARCH bits are reserved for architecture-specific
142 #define PERF_EVENT_FLAG_ARCH 0x000fffff
143 #define PERF_EVENT_FLAG_USER_READ_CNT 0x80000000
145 static_assert((PERF_EVENT_FLAG_USER_READ_CNT & PERF_EVENT_FLAG_ARCH) == 0);
148 * struct hw_perf_event - performance event hardware details:
150 struct hw_perf_event {
151 #ifdef CONFIG_PERF_EVENTS
153 struct { /* hardware */
156 unsigned long config_base;
157 unsigned long event_base;
158 int event_base_rdpmc;
163 struct hw_perf_event_extra extra_reg;
164 struct hw_perf_event_extra branch_reg;
166 struct { /* software */
167 struct hrtimer hrtimer;
169 struct { /* tracepoint */
170 /* for tp_event->class */
171 struct list_head tp_list;
173 struct { /* amd_power */
177 #ifdef CONFIG_HAVE_HW_BREAKPOINT
178 struct { /* breakpoint */
180 * Crufty hack to avoid the chicken and egg
181 * problem hw_breakpoint has with context
182 * creation and event initalization.
184 struct arch_hw_breakpoint info;
185 struct rhlist_head bp_list;
188 struct { /* amd_iommu */
197 * If the event is a per task event, this will point to the task in
198 * question. See the comment in perf_event_alloc().
200 struct task_struct *target;
203 * PMU would store hardware filter configuration
208 /* Last sync'ed generation of filters */
209 unsigned long addr_filters_gen;
212 * hw_perf_event::state flags; used to track the PERF_EF_* state.
214 #define PERF_HES_STOPPED 0x01 /* the counter is stopped */
215 #define PERF_HES_UPTODATE 0x02 /* event->count up-to-date */
216 #define PERF_HES_ARCH 0x04
221 * The last observed hardware counter value, updated with a
222 * local64_cmpxchg() such that pmu::read() can be called nested.
224 local64_t prev_count;
227 * The period to start the next sample with.
232 struct { /* Sampling */
234 * The period we started this sample with.
239 * However much is left of the current period;
240 * note that this is a full 64bit value and
241 * allows for generation of periods longer
242 * than hardware might allow.
244 local64_t period_left;
246 struct { /* Topdown events counting for context switch */
253 * State for throttling the event, see __perf_event_overflow() and
254 * perf_adjust_freq_unthr_context().
260 * State for freq target events, see __perf_event_overflow() and
261 * perf_adjust_freq_unthr_context().
264 u64 freq_count_stamp;
271 * Common implementation detail of pmu::{start,commit,cancel}_txn
273 #define PERF_PMU_TXN_ADD 0x1 /* txn to add/schedule event on PMU */
274 #define PERF_PMU_TXN_READ 0x2 /* txn to read event group from PMU */
277 * pmu::capabilities flags
279 #define PERF_PMU_CAP_NO_INTERRUPT 0x0001
280 #define PERF_PMU_CAP_NO_NMI 0x0002
281 #define PERF_PMU_CAP_AUX_NO_SG 0x0004
282 #define PERF_PMU_CAP_EXTENDED_REGS 0x0008
283 #define PERF_PMU_CAP_EXCLUSIVE 0x0010
284 #define PERF_PMU_CAP_ITRACE 0x0020
285 #define PERF_PMU_CAP_HETEROGENEOUS_CPUS 0x0040
286 #define PERF_PMU_CAP_NO_EXCLUDE 0x0080
287 #define PERF_PMU_CAP_AUX_OUTPUT 0x0100
288 #define PERF_PMU_CAP_EXTENDED_HW_TYPE 0x0200
290 struct perf_output_handle;
293 * struct pmu - generic performance monitoring unit
296 struct list_head entry;
298 struct module *module;
300 const struct attribute_group **attr_groups;
301 const struct attribute_group **attr_update;
306 * various common per-pmu feature flags
310 int __percpu *pmu_disable_count;
311 struct perf_cpu_context __percpu *pmu_cpu_context;
312 atomic_t exclusive_cnt; /* < 0: cpu; > 0: tsk */
314 int hrtimer_interval_ms;
316 /* number of address filters this PMU can do */
317 unsigned int nr_addr_filters;
320 * Fully disable/enable this PMU, can be used to protect from the PMI
321 * as well as for lazy/batch writing of the MSRs.
323 void (*pmu_enable) (struct pmu *pmu); /* optional */
324 void (*pmu_disable) (struct pmu *pmu); /* optional */
327 * Try and initialize the event for this PMU.
330 * -ENOENT -- @event is not for this PMU
332 * -ENODEV -- @event is for this PMU but PMU not present
333 * -EBUSY -- @event is for this PMU but PMU temporarily unavailable
334 * -EINVAL -- @event is for this PMU but @event is not valid
335 * -EOPNOTSUPP -- @event is for this PMU, @event is valid, but not supported
336 * -EACCES -- @event is for this PMU, @event is valid, but no privileges
338 * 0 -- @event is for this PMU and valid
340 * Other error return values are allowed.
342 int (*event_init) (struct perf_event *event);
345 * Notification that the event was mapped or unmapped. Called
346 * in the context of the mapping task.
348 void (*event_mapped) (struct perf_event *event, struct mm_struct *mm); /* optional */
349 void (*event_unmapped) (struct perf_event *event, struct mm_struct *mm); /* optional */
352 * Flags for ->add()/->del()/ ->start()/->stop(). There are
353 * matching hw_perf_event::state flags.
355 #define PERF_EF_START 0x01 /* start the counter when adding */
356 #define PERF_EF_RELOAD 0x02 /* reload the counter when starting */
357 #define PERF_EF_UPDATE 0x04 /* update the counter when stopping */
360 * Adds/Removes a counter to/from the PMU, can be done inside a
361 * transaction, see the ->*_txn() methods.
363 * The add/del callbacks will reserve all hardware resources required
364 * to service the event, this includes any counter constraint
367 * Called with IRQs disabled and the PMU disabled on the CPU the event
370 * ->add() called without PERF_EF_START should result in the same state
371 * as ->add() followed by ->stop().
373 * ->del() must always PERF_EF_UPDATE stop an event. If it calls
374 * ->stop() that must deal with already being stopped without
377 int (*add) (struct perf_event *event, int flags);
378 void (*del) (struct perf_event *event, int flags);
381 * Starts/Stops a counter present on the PMU.
383 * The PMI handler should stop the counter when perf_event_overflow()
384 * returns !0. ->start() will be used to continue.
386 * Also used to change the sample period.
388 * Called with IRQs disabled and the PMU disabled on the CPU the event
389 * is on -- will be called from NMI context with the PMU generates
392 * ->stop() with PERF_EF_UPDATE will read the counter and update
393 * period/count values like ->read() would.
395 * ->start() with PERF_EF_RELOAD will reprogram the counter
396 * value, must be preceded by a ->stop() with PERF_EF_UPDATE.
398 void (*start) (struct perf_event *event, int flags);
399 void (*stop) (struct perf_event *event, int flags);
402 * Updates the counter value of the event.
404 * For sampling capable PMUs this will also update the software period
405 * hw_perf_event::period_left field.
407 void (*read) (struct perf_event *event);
410 * Group events scheduling is treated as a transaction, add
411 * group events as a whole and perform one schedulability test.
412 * If the test fails, roll back the whole group
414 * Start the transaction, after this ->add() doesn't need to
415 * do schedulability tests.
419 void (*start_txn) (struct pmu *pmu, unsigned int txn_flags);
421 * If ->start_txn() disabled the ->add() schedulability test
422 * then ->commit_txn() is required to perform one. On success
423 * the transaction is closed. On error the transaction is kept
424 * open until ->cancel_txn() is called.
428 int (*commit_txn) (struct pmu *pmu);
430 * Will cancel the transaction, assumes ->del() is called
431 * for each successful ->add() during the transaction.
435 void (*cancel_txn) (struct pmu *pmu);
438 * Will return the value for perf_event_mmap_page::index for this event,
439 * if no implementation is provided it will default to: event->hw.idx + 1.
441 int (*event_idx) (struct perf_event *event); /*optional */
444 * context-switches callback
446 void (*sched_task) (struct perf_event_context *ctx,
450 * Kmem cache of PMU specific data
452 struct kmem_cache *task_ctx_cache;
455 * PMU specific parts of task perf event context (i.e. ctx->task_ctx_data)
456 * can be synchronized using this function. See Intel LBR callstack support
457 * implementation and Perf core context switch handling callbacks for usage
460 void (*swap_task_ctx) (struct perf_event_context *prev,
461 struct perf_event_context *next);
465 * Set up pmu-private data structures for an AUX area
467 void *(*setup_aux) (struct perf_event *event, void **pages,
468 int nr_pages, bool overwrite);
472 * Free pmu-private AUX data structures
474 void (*free_aux) (void *aux); /* optional */
477 * Take a snapshot of the AUX buffer without touching the event
478 * state, so that preempting ->start()/->stop() callbacks does
479 * not interfere with their logic. Called in PMI context.
481 * Returns the size of AUX data copied to the output handle.
485 long (*snapshot_aux) (struct perf_event *event,
486 struct perf_output_handle *handle,
490 * Validate address range filters: make sure the HW supports the
491 * requested configuration and number of filters; return 0 if the
492 * supplied filters are valid, -errno otherwise.
494 * Runs in the context of the ioctl()ing process and is not serialized
495 * with the rest of the PMU callbacks.
497 int (*addr_filters_validate) (struct list_head *filters);
501 * Synchronize address range filter configuration:
502 * translate hw-agnostic filters into hardware configuration in
503 * event::hw::addr_filters.
505 * Runs as a part of filter sync sequence that is done in ->start()
506 * callback by calling perf_event_addr_filters_sync().
508 * May (and should) traverse event::addr_filters::list, for which its
509 * caller provides necessary serialization.
511 void (*addr_filters_sync) (struct perf_event *event);
515 * Check if event can be used for aux_output purposes for
516 * events of this PMU.
518 * Runs from perf_event_open(). Should return 0 for "no match"
519 * or non-zero for "match".
521 int (*aux_output_match) (struct perf_event *event);
525 * Filter events for PMU-specific reasons.
527 int (*filter_match) (struct perf_event *event); /* optional */
530 * Check period value for PERF_EVENT_IOC_PERIOD ioctl.
532 int (*check_period) (struct perf_event *event, u64 value); /* optional */
535 enum perf_addr_filter_action_t {
536 PERF_ADDR_FILTER_ACTION_STOP = 0,
537 PERF_ADDR_FILTER_ACTION_START,
538 PERF_ADDR_FILTER_ACTION_FILTER,
542 * struct perf_addr_filter - address range filter definition
543 * @entry: event's filter list linkage
544 * @path: object file's path for file-based filters
545 * @offset: filter range offset
546 * @size: filter range size (size==0 means single address trigger)
547 * @action: filter/start/stop
549 * This is a hardware-agnostic filter configuration as specified by the user.
551 struct perf_addr_filter {
552 struct list_head entry;
554 unsigned long offset;
556 enum perf_addr_filter_action_t action;
560 * struct perf_addr_filters_head - container for address range filters
561 * @list: list of filters for this event
562 * @lock: spinlock that serializes accesses to the @list and event's
563 * (and its children's) filter generations.
564 * @nr_file_filters: number of file-based filters
566 * A child event will use parent's @list (and therefore @lock), so they are
567 * bundled together; see perf_event_addr_filters().
569 struct perf_addr_filters_head {
570 struct list_head list;
572 unsigned int nr_file_filters;
575 struct perf_addr_filter_range {
581 * enum perf_event_state - the states of an event:
583 enum perf_event_state {
584 PERF_EVENT_STATE_DEAD = -4,
585 PERF_EVENT_STATE_EXIT = -3,
586 PERF_EVENT_STATE_ERROR = -2,
587 PERF_EVENT_STATE_OFF = -1,
588 PERF_EVENT_STATE_INACTIVE = 0,
589 PERF_EVENT_STATE_ACTIVE = 1,
593 struct perf_sample_data;
595 typedef void (*perf_overflow_handler_t)(struct perf_event *,
596 struct perf_sample_data *,
597 struct pt_regs *regs);
600 * Event capabilities. For event_caps and groups caps.
602 * PERF_EV_CAP_SOFTWARE: Is a software event.
603 * PERF_EV_CAP_READ_ACTIVE_PKG: A CPU event (or cgroup event) that can be read
604 * from any CPU in the package where it is active.
605 * PERF_EV_CAP_SIBLING: An event with this flag must be a group sibling and
606 * cannot be a group leader. If an event with this flag is detached from the
607 * group it is scheduled out and moved into an unrecoverable ERROR state.
609 #define PERF_EV_CAP_SOFTWARE BIT(0)
610 #define PERF_EV_CAP_READ_ACTIVE_PKG BIT(1)
611 #define PERF_EV_CAP_SIBLING BIT(2)
613 #define SWEVENT_HLIST_BITS 8
614 #define SWEVENT_HLIST_SIZE (1 << SWEVENT_HLIST_BITS)
616 struct swevent_hlist {
617 struct hlist_head heads[SWEVENT_HLIST_SIZE];
618 struct rcu_head rcu_head;
621 #define PERF_ATTACH_CONTEXT 0x01
622 #define PERF_ATTACH_GROUP 0x02
623 #define PERF_ATTACH_TASK 0x04
624 #define PERF_ATTACH_TASK_DATA 0x08
625 #define PERF_ATTACH_ITRACE 0x10
626 #define PERF_ATTACH_SCHED_CB 0x20
627 #define PERF_ATTACH_CHILD 0x40
633 struct pmu_event_list {
635 struct list_head list;
639 * event->sibling_list is modified whole holding both ctx->lock and ctx->mutex
640 * as such iteration must hold either lock. However, since ctx->lock is an IRQ
641 * safe lock, and is only held by the CPU doing the modification, having IRQs
642 * disabled is sufficient since it will hold-off the IPIs.
644 #ifdef CONFIG_PROVE_LOCKING
645 #define lockdep_assert_event_ctx(event) \
646 WARN_ON_ONCE(__lockdep_enabled && \
647 (this_cpu_read(hardirqs_enabled) && \
648 lockdep_is_held(&(event)->ctx->mutex) != LOCK_STATE_HELD))
650 #define lockdep_assert_event_ctx(event)
653 #define for_each_sibling_event(sibling, event) \
654 lockdep_assert_event_ctx(event); \
655 if ((event)->group_leader == (event)) \
656 list_for_each_entry((sibling), &(event)->sibling_list, sibling_list)
659 * struct perf_event - performance event kernel representation:
662 #ifdef CONFIG_PERF_EVENTS
664 * entry onto perf_event_context::event_list;
665 * modifications require ctx->lock
666 * RCU safe iterations.
668 struct list_head event_entry;
671 * Locked for modification by both ctx->mutex and ctx->lock; holding
672 * either sufficies for read.
674 struct list_head sibling_list;
675 struct list_head active_list;
677 * Node on the pinned or flexible tree located at the event context;
679 struct rb_node group_node;
682 * We need storage to track the entries in perf_pmu_migrate_context; we
683 * cannot use the event_entry because of RCU and we want to keep the
684 * group in tact which avoids us using the other two entries.
686 struct list_head migrate_entry;
688 struct hlist_node hlist_entry;
689 struct list_head active_entry;
692 /* Not serialized. Only written during event initialization. */
694 /* The cumulative AND of all event_caps for events in this group. */
697 struct perf_event *group_leader;
701 enum perf_event_state state;
702 unsigned int attach_state;
704 atomic64_t child_count;
707 * These are the total time in nanoseconds that the event
708 * has been enabled (i.e. eligible to run, and the task has
709 * been scheduled in, if this is a per-task event)
710 * and running (scheduled onto the CPU), respectively.
712 u64 total_time_enabled;
713 u64 total_time_running;
716 struct perf_event_attr attr;
720 struct hw_perf_event hw;
722 struct perf_event_context *ctx;
723 atomic_long_t refcount;
726 * These accumulate total time (in nanoseconds) that children
727 * events have been enabled and running, respectively.
729 atomic64_t child_total_time_enabled;
730 atomic64_t child_total_time_running;
733 * Protect attach/detach and child_list:
735 struct mutex child_mutex;
736 struct list_head child_list;
737 struct perf_event *parent;
742 struct list_head owner_entry;
743 struct task_struct *owner;
746 struct mutex mmap_mutex;
749 struct perf_buffer *rb;
750 struct list_head rb_entry;
751 unsigned long rcu_batches;
755 wait_queue_head_t waitq;
756 struct fasync_struct *fasync;
758 /* delayed work for NMIs and such */
759 unsigned int pending_wakeup;
760 unsigned int pending_kill;
761 unsigned int pending_disable;
762 unsigned int pending_sigtrap;
763 unsigned long pending_addr; /* SIGTRAP */
764 struct irq_work pending_irq;
765 struct callback_head pending_task;
766 unsigned int pending_work;
768 atomic_t event_limit;
770 /* address range filters */
771 struct perf_addr_filters_head addr_filters;
772 /* vma address array for file-based filders */
773 struct perf_addr_filter_range *addr_filter_ranges;
774 unsigned long addr_filters_gen;
776 /* for aux_output events */
777 struct perf_event *aux_event;
779 void (*destroy)(struct perf_event *);
780 struct rcu_head rcu_head;
782 struct pid_namespace *ns;
785 atomic64_t lost_samples;
788 perf_overflow_handler_t overflow_handler;
789 void *overflow_handler_context;
790 #ifdef CONFIG_BPF_SYSCALL
791 perf_overflow_handler_t orig_overflow_handler;
792 struct bpf_prog *prog;
796 #ifdef CONFIG_EVENT_TRACING
797 struct trace_event_call *tp_event;
798 struct event_filter *filter;
799 #ifdef CONFIG_FUNCTION_TRACER
800 struct ftrace_ops ftrace_ops;
804 #ifdef CONFIG_CGROUP_PERF
805 struct perf_cgroup *cgrp; /* cgroup event is attach to */
808 #ifdef CONFIG_SECURITY
811 struct list_head sb_list;
812 #endif /* CONFIG_PERF_EVENTS */
816 struct perf_event_groups {
822 * struct perf_event_context - event context structure
824 * Used as a container for task events and CPU events as well:
826 struct perf_event_context {
829 * Protect the states of the events in the list,
830 * nr_active, and the list:
834 * Protect the list of events. Locking either mutex or lock
835 * is sufficient to ensure the list doesn't change; to change
836 * the list you need to lock both the mutex and the spinlock.
840 struct list_head active_ctx_list;
841 struct perf_event_groups pinned_groups;
842 struct perf_event_groups flexible_groups;
843 struct list_head event_list;
845 struct list_head pinned_active;
846 struct list_head flexible_active;
856 * Set when nr_events != nr_active, except tolerant to events not
857 * necessary to be active due to scheduling constraints, such as cgroups.
859 int rotate_necessary;
861 struct task_struct *task;
864 * Context clock, runs when context enabled.
871 * These fields let us detect when two contexts have both
872 * been cloned (inherited) from a common ancestor.
874 struct perf_event_context *parent_ctx;
878 #ifdef CONFIG_CGROUP_PERF
879 int nr_cgroups; /* cgroup evts */
881 void *task_ctx_data; /* pmu specific data */
882 struct rcu_head rcu_head;
885 * Sum (event->pending_sigtrap + event->pending_work)
887 * The SIGTRAP is targeted at ctx->task, as such it won't do changing
888 * that until the signal is delivered.
894 * Number of contexts where an event can trigger:
895 * task, softirq, hardirq, nmi.
897 #define PERF_NR_CONTEXTS 4
900 * struct perf_cpu_context - per cpu event context structure
902 struct perf_cpu_context {
903 struct perf_event_context ctx;
904 struct perf_event_context *task_ctx;
908 raw_spinlock_t hrtimer_lock;
909 struct hrtimer hrtimer;
910 ktime_t hrtimer_interval;
911 unsigned int hrtimer_active;
913 #ifdef CONFIG_CGROUP_PERF
914 struct perf_cgroup *cgrp;
915 struct list_head cgrp_cpuctx_entry;
918 struct list_head sched_cb_entry;
923 * Per-CPU storage for iterators used in visit_groups_merge. The default
924 * storage is of size 2 to hold the CPU and any CPU event iterators.
927 struct perf_event **heap;
928 struct perf_event *heap_default[2];
931 struct perf_output_handle {
932 struct perf_event *event;
933 struct perf_buffer *rb;
934 unsigned long wakeup;
944 struct bpf_perf_event_data_kern {
945 bpf_user_pt_regs_t *regs;
946 struct perf_sample_data *data;
947 struct perf_event *event;
950 #ifdef CONFIG_CGROUP_PERF
953 * perf_cgroup_info keeps track of time_enabled for a cgroup.
954 * This is a per-cpu dynamically allocated data structure.
956 struct perf_cgroup_info {
964 struct cgroup_subsys_state css;
965 struct perf_cgroup_info __percpu *info;
969 * Must ensure cgroup is pinned (css_get) before calling
970 * this function. In other words, we cannot call this function
971 * if there is no cgroup event for the current CPU context.
973 static inline struct perf_cgroup *
974 perf_cgroup_from_task(struct task_struct *task, struct perf_event_context *ctx)
976 return container_of(task_css_check(task, perf_event_cgrp_id,
977 ctx ? lockdep_is_held(&ctx->lock)
979 struct perf_cgroup, css);
981 #endif /* CONFIG_CGROUP_PERF */
983 #ifdef CONFIG_PERF_EVENTS
985 extern void *perf_aux_output_begin(struct perf_output_handle *handle,
986 struct perf_event *event);
987 extern void perf_aux_output_end(struct perf_output_handle *handle,
989 extern int perf_aux_output_skip(struct perf_output_handle *handle,
991 extern void *perf_get_aux(struct perf_output_handle *handle);
992 extern void perf_aux_output_flag(struct perf_output_handle *handle, u64 flags);
993 extern void perf_event_itrace_started(struct perf_event *event);
995 extern int perf_pmu_register(struct pmu *pmu, const char *name, int type);
996 extern void perf_pmu_unregister(struct pmu *pmu);
998 extern void __perf_event_task_sched_in(struct task_struct *prev,
999 struct task_struct *task);
1000 extern void __perf_event_task_sched_out(struct task_struct *prev,
1001 struct task_struct *next);
1002 extern int perf_event_init_task(struct task_struct *child, u64 clone_flags);
1003 extern void perf_event_exit_task(struct task_struct *child);
1004 extern void perf_event_free_task(struct task_struct *task);
1005 extern void perf_event_delayed_put(struct task_struct *task);
1006 extern struct file *perf_event_get(unsigned int fd);
1007 extern const struct perf_event *perf_get_event(struct file *file);
1008 extern const struct perf_event_attr *perf_event_attrs(struct perf_event *event);
1009 extern void perf_event_print_debug(void);
1010 extern void perf_pmu_disable(struct pmu *pmu);
1011 extern void perf_pmu_enable(struct pmu *pmu);
1012 extern void perf_sched_cb_dec(struct pmu *pmu);
1013 extern void perf_sched_cb_inc(struct pmu *pmu);
1014 extern int perf_event_task_disable(void);
1015 extern int perf_event_task_enable(void);
1017 extern void perf_pmu_resched(struct pmu *pmu);
1019 extern int perf_event_refresh(struct perf_event *event, int refresh);
1020 extern void perf_event_update_userpage(struct perf_event *event);
1021 extern int perf_event_release_kernel(struct perf_event *event);
1022 extern struct perf_event *
1023 perf_event_create_kernel_counter(struct perf_event_attr *attr,
1025 struct task_struct *task,
1026 perf_overflow_handler_t callback,
1028 extern void perf_pmu_migrate_context(struct pmu *pmu,
1029 int src_cpu, int dst_cpu);
1030 int perf_event_read_local(struct perf_event *event, u64 *value,
1031 u64 *enabled, u64 *running);
1032 extern u64 perf_event_read_value(struct perf_event *event,
1033 u64 *enabled, u64 *running);
1036 struct perf_sample_data {
1038 * Fields set by perf_sample_data_init(), group so as to
1039 * minimize the cachelines touched.
1045 * The other fields, optionally {set,used} by
1046 * perf_{prepare,output}_sample().
1048 struct perf_branch_stack *br_stack;
1049 union perf_sample_weight weight;
1050 union perf_mem_data_src data_src;
1053 struct perf_raw_record *raw;
1068 struct perf_callchain_entry *callchain;
1071 struct perf_regs regs_user;
1072 struct perf_regs regs_intr;
1073 u64 stack_user_size;
1079 } ____cacheline_aligned;
1081 /* default value for data source */
1082 #define PERF_MEM_NA (PERF_MEM_S(OP, NA) |\
1083 PERF_MEM_S(LVL, NA) |\
1084 PERF_MEM_S(SNOOP, NA) |\
1085 PERF_MEM_S(LOCK, NA) |\
1086 PERF_MEM_S(TLB, NA))
1088 static inline void perf_sample_data_init(struct perf_sample_data *data,
1089 u64 addr, u64 period)
1091 /* remaining struct members initialized in perf_prepare_sample() */
1092 data->sample_flags = PERF_SAMPLE_PERIOD;
1093 data->period = period;
1097 data->sample_flags |= PERF_SAMPLE_ADDR;
1102 * Clear all bitfields in the perf_branch_entry.
1103 * The to and from fields are not cleared because they are
1104 * systematically modified by caller.
1106 static inline void perf_clear_branch_entry_bitfields(struct perf_branch_entry *br)
1114 br->spec = PERF_BR_SPEC_NA;
1118 extern void perf_output_sample(struct perf_output_handle *handle,
1119 struct perf_event_header *header,
1120 struct perf_sample_data *data,
1121 struct perf_event *event);
1122 extern void perf_prepare_sample(struct perf_event_header *header,
1123 struct perf_sample_data *data,
1124 struct perf_event *event,
1125 struct pt_regs *regs);
1127 extern int perf_event_overflow(struct perf_event *event,
1128 struct perf_sample_data *data,
1129 struct pt_regs *regs);
1131 extern void perf_event_output_forward(struct perf_event *event,
1132 struct perf_sample_data *data,
1133 struct pt_regs *regs);
1134 extern void perf_event_output_backward(struct perf_event *event,
1135 struct perf_sample_data *data,
1136 struct pt_regs *regs);
1137 extern int perf_event_output(struct perf_event *event,
1138 struct perf_sample_data *data,
1139 struct pt_regs *regs);
1142 is_default_overflow_handler(struct perf_event *event)
1144 if (likely(event->overflow_handler == perf_event_output_forward))
1146 if (unlikely(event->overflow_handler == perf_event_output_backward))
1152 perf_event_header__init_id(struct perf_event_header *header,
1153 struct perf_sample_data *data,
1154 struct perf_event *event);
1156 perf_event__output_id_sample(struct perf_event *event,
1157 struct perf_output_handle *handle,
1158 struct perf_sample_data *sample);
1161 perf_log_lost_samples(struct perf_event *event, u64 lost);
1163 static inline bool event_has_any_exclude_flag(struct perf_event *event)
1165 struct perf_event_attr *attr = &event->attr;
1167 return attr->exclude_idle || attr->exclude_user ||
1168 attr->exclude_kernel || attr->exclude_hv ||
1169 attr->exclude_guest || attr->exclude_host;
1172 static inline bool is_sampling_event(struct perf_event *event)
1174 return event->attr.sample_period != 0;
1178 * Return 1 for a software event, 0 for a hardware event
1180 static inline int is_software_event(struct perf_event *event)
1182 return event->event_caps & PERF_EV_CAP_SOFTWARE;
1186 * Return 1 for event in sw context, 0 for event in hw context
1188 static inline int in_software_context(struct perf_event *event)
1190 return event->ctx->pmu->task_ctx_nr == perf_sw_context;
1193 static inline int is_exclusive_pmu(struct pmu *pmu)
1195 return pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE;
1198 extern struct static_key perf_swevent_enabled[PERF_COUNT_SW_MAX];
1200 extern void ___perf_sw_event(u32, u64, struct pt_regs *, u64);
1201 extern void __perf_sw_event(u32, u64, struct pt_regs *, u64);
1203 #ifndef perf_arch_fetch_caller_regs
1204 static inline void perf_arch_fetch_caller_regs(struct pt_regs *regs, unsigned long ip) { }
1208 * When generating a perf sample in-line, instead of from an interrupt /
1209 * exception, we lack a pt_regs. This is typically used from software events
1210 * like: SW_CONTEXT_SWITCHES, SW_MIGRATIONS and the tie-in with tracepoints.
1212 * We typically don't need a full set, but (for x86) do require:
1213 * - ip for PERF_SAMPLE_IP
1214 * - cs for user_mode() tests
1215 * - sp for PERF_SAMPLE_CALLCHAIN
1216 * - eflags for MISC bits and CALLCHAIN (see: perf_hw_regs())
1218 * NOTE: assumes @regs is otherwise already 0 filled; this is important for
1219 * things like PERF_SAMPLE_REGS_INTR.
1221 static inline void perf_fetch_caller_regs(struct pt_regs *regs)
1223 perf_arch_fetch_caller_regs(regs, CALLER_ADDR0);
1226 static __always_inline void
1227 perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr)
1229 if (static_key_false(&perf_swevent_enabled[event_id]))
1230 __perf_sw_event(event_id, nr, regs, addr);
1233 DECLARE_PER_CPU(struct pt_regs, __perf_regs[4]);
1236 * 'Special' version for the scheduler, it hard assumes no recursion,
1237 * which is guaranteed by us not actually scheduling inside other swevents
1238 * because those disable preemption.
1240 static __always_inline void __perf_sw_event_sched(u32 event_id, u64 nr, u64 addr)
1242 struct pt_regs *regs = this_cpu_ptr(&__perf_regs[0]);
1244 perf_fetch_caller_regs(regs);
1245 ___perf_sw_event(event_id, nr, regs, addr);
1248 extern struct static_key_false perf_sched_events;
1250 static __always_inline bool __perf_sw_enabled(int swevt)
1252 return static_key_false(&perf_swevent_enabled[swevt]);
1255 static inline void perf_event_task_migrate(struct task_struct *task)
1257 if (__perf_sw_enabled(PERF_COUNT_SW_CPU_MIGRATIONS))
1258 task->sched_migrated = 1;
1261 static inline void perf_event_task_sched_in(struct task_struct *prev,
1262 struct task_struct *task)
1264 if (static_branch_unlikely(&perf_sched_events))
1265 __perf_event_task_sched_in(prev, task);
1267 if (__perf_sw_enabled(PERF_COUNT_SW_CPU_MIGRATIONS) &&
1268 task->sched_migrated) {
1269 __perf_sw_event_sched(PERF_COUNT_SW_CPU_MIGRATIONS, 1, 0);
1270 task->sched_migrated = 0;
1274 static inline void perf_event_task_sched_out(struct task_struct *prev,
1275 struct task_struct *next)
1277 if (__perf_sw_enabled(PERF_COUNT_SW_CONTEXT_SWITCHES))
1278 __perf_sw_event_sched(PERF_COUNT_SW_CONTEXT_SWITCHES, 1, 0);
1280 #ifdef CONFIG_CGROUP_PERF
1281 if (__perf_sw_enabled(PERF_COUNT_SW_CGROUP_SWITCHES) &&
1282 perf_cgroup_from_task(prev, NULL) !=
1283 perf_cgroup_from_task(next, NULL))
1284 __perf_sw_event_sched(PERF_COUNT_SW_CGROUP_SWITCHES, 1, 0);
1287 if (static_branch_unlikely(&perf_sched_events))
1288 __perf_event_task_sched_out(prev, next);
1291 extern void perf_event_mmap(struct vm_area_struct *vma);
1293 extern void perf_event_ksymbol(u16 ksym_type, u64 addr, u32 len,
1294 bool unregister, const char *sym);
1295 extern void perf_event_bpf_event(struct bpf_prog *prog,
1296 enum perf_bpf_event_type type,
1299 #ifdef CONFIG_GUEST_PERF_EVENTS
1300 extern struct perf_guest_info_callbacks __rcu *perf_guest_cbs;
1302 DECLARE_STATIC_CALL(__perf_guest_state, *perf_guest_cbs->state);
1303 DECLARE_STATIC_CALL(__perf_guest_get_ip, *perf_guest_cbs->get_ip);
1304 DECLARE_STATIC_CALL(__perf_guest_handle_intel_pt_intr, *perf_guest_cbs->handle_intel_pt_intr);
1306 static inline unsigned int perf_guest_state(void)
1308 return static_call(__perf_guest_state)();
1310 static inline unsigned long perf_guest_get_ip(void)
1312 return static_call(__perf_guest_get_ip)();
1314 static inline unsigned int perf_guest_handle_intel_pt_intr(void)
1316 return static_call(__perf_guest_handle_intel_pt_intr)();
1318 extern void perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs);
1319 extern void perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs);
1321 static inline unsigned int perf_guest_state(void) { return 0; }
1322 static inline unsigned long perf_guest_get_ip(void) { return 0; }
1323 static inline unsigned int perf_guest_handle_intel_pt_intr(void) { return 0; }
1324 #endif /* CONFIG_GUEST_PERF_EVENTS */
1326 extern void perf_event_exec(void);
1327 extern void perf_event_comm(struct task_struct *tsk, bool exec);
1328 extern void perf_event_namespaces(struct task_struct *tsk);
1329 extern void perf_event_fork(struct task_struct *tsk);
1330 extern void perf_event_text_poke(const void *addr,
1331 const void *old_bytes, size_t old_len,
1332 const void *new_bytes, size_t new_len);
1335 DECLARE_PER_CPU(struct perf_callchain_entry, perf_callchain_entry);
1337 extern void perf_callchain_user(struct perf_callchain_entry_ctx *entry, struct pt_regs *regs);
1338 extern void perf_callchain_kernel(struct perf_callchain_entry_ctx *entry, struct pt_regs *regs);
1339 extern struct perf_callchain_entry *
1340 get_perf_callchain(struct pt_regs *regs, u32 init_nr, bool kernel, bool user,
1341 u32 max_stack, bool crosstask, bool add_mark);
1342 extern struct perf_callchain_entry *perf_callchain(struct perf_event *event, struct pt_regs *regs);
1343 extern int get_callchain_buffers(int max_stack);
1344 extern void put_callchain_buffers(void);
1345 extern struct perf_callchain_entry *get_callchain_entry(int *rctx);
1346 extern void put_callchain_entry(int rctx);
1348 extern int sysctl_perf_event_max_stack;
1349 extern int sysctl_perf_event_max_contexts_per_stack;
1351 static inline int perf_callchain_store_context(struct perf_callchain_entry_ctx *ctx, u64 ip)
1353 if (ctx->contexts < sysctl_perf_event_max_contexts_per_stack) {
1354 struct perf_callchain_entry *entry = ctx->entry;
1355 entry->ip[entry->nr++] = ip;
1359 ctx->contexts_maxed = true;
1360 return -1; /* no more room, stop walking the stack */
1364 static inline int perf_callchain_store(struct perf_callchain_entry_ctx *ctx, u64 ip)
1366 if (ctx->nr < ctx->max_stack && !ctx->contexts_maxed) {
1367 struct perf_callchain_entry *entry = ctx->entry;
1368 entry->ip[entry->nr++] = ip;
1372 return -1; /* no more room, stop walking the stack */
1376 extern int sysctl_perf_event_paranoid;
1377 extern int sysctl_perf_event_mlock;
1378 extern int sysctl_perf_event_sample_rate;
1379 extern int sysctl_perf_cpu_time_max_percent;
1381 extern void perf_sample_event_took(u64 sample_len_ns);
1383 int perf_proc_update_handler(struct ctl_table *table, int write,
1384 void *buffer, size_t *lenp, loff_t *ppos);
1385 int perf_cpu_time_max_percent_handler(struct ctl_table *table, int write,
1386 void *buffer, size_t *lenp, loff_t *ppos);
1387 int perf_event_max_stack_handler(struct ctl_table *table, int write,
1388 void *buffer, size_t *lenp, loff_t *ppos);
1390 /* Access to perf_event_open(2) syscall. */
1391 #define PERF_SECURITY_OPEN 0
1393 /* Finer grained perf_event_open(2) access control. */
1394 #define PERF_SECURITY_CPU 1
1395 #define PERF_SECURITY_KERNEL 2
1396 #define PERF_SECURITY_TRACEPOINT 3
1398 static inline int perf_is_paranoid(void)
1400 return sysctl_perf_event_paranoid > -1;
1403 static inline int perf_allow_kernel(struct perf_event_attr *attr)
1405 if (sysctl_perf_event_paranoid > 1 && !perfmon_capable())
1408 return security_perf_event_open(attr, PERF_SECURITY_KERNEL);
1411 static inline int perf_allow_cpu(struct perf_event_attr *attr)
1413 if (sysctl_perf_event_paranoid > 0 && !perfmon_capable())
1416 return security_perf_event_open(attr, PERF_SECURITY_CPU);
1419 static inline int perf_allow_tracepoint(struct perf_event_attr *attr)
1421 if (sysctl_perf_event_paranoid > -1 && !perfmon_capable())
1424 return security_perf_event_open(attr, PERF_SECURITY_TRACEPOINT);
1427 extern void perf_event_init(void);
1428 extern void perf_tp_event(u16 event_type, u64 count, void *record,
1429 int entry_size, struct pt_regs *regs,
1430 struct hlist_head *head, int rctx,
1431 struct task_struct *task);
1432 extern void perf_bp_event(struct perf_event *event, void *data);
1434 #ifndef perf_misc_flags
1435 # define perf_misc_flags(regs) \
1436 (user_mode(regs) ? PERF_RECORD_MISC_USER : PERF_RECORD_MISC_KERNEL)
1437 # define perf_instruction_pointer(regs) instruction_pointer(regs)
1439 #ifndef perf_arch_bpf_user_pt_regs
1440 # define perf_arch_bpf_user_pt_regs(regs) regs
1443 static inline bool has_branch_stack(struct perf_event *event)
1445 return event->attr.sample_type & PERF_SAMPLE_BRANCH_STACK;
1448 static inline bool needs_branch_stack(struct perf_event *event)
1450 return event->attr.branch_sample_type != 0;
1453 static inline bool has_aux(struct perf_event *event)
1455 return event->pmu->setup_aux;
1458 static inline bool is_write_backward(struct perf_event *event)
1460 return !!event->attr.write_backward;
1463 static inline bool has_addr_filter(struct perf_event *event)
1465 return event->pmu->nr_addr_filters;
1469 * An inherited event uses parent's filters
1471 static inline struct perf_addr_filters_head *
1472 perf_event_addr_filters(struct perf_event *event)
1474 struct perf_addr_filters_head *ifh = &event->addr_filters;
1477 ifh = &event->parent->addr_filters;
1482 extern void perf_event_addr_filters_sync(struct perf_event *event);
1483 extern void perf_report_aux_output_id(struct perf_event *event, u64 hw_id);
1485 extern int perf_output_begin(struct perf_output_handle *handle,
1486 struct perf_sample_data *data,
1487 struct perf_event *event, unsigned int size);
1488 extern int perf_output_begin_forward(struct perf_output_handle *handle,
1489 struct perf_sample_data *data,
1490 struct perf_event *event,
1492 extern int perf_output_begin_backward(struct perf_output_handle *handle,
1493 struct perf_sample_data *data,
1494 struct perf_event *event,
1497 extern void perf_output_end(struct perf_output_handle *handle);
1498 extern unsigned int perf_output_copy(struct perf_output_handle *handle,
1499 const void *buf, unsigned int len);
1500 extern unsigned int perf_output_skip(struct perf_output_handle *handle,
1502 extern long perf_output_copy_aux(struct perf_output_handle *aux_handle,
1503 struct perf_output_handle *handle,
1504 unsigned long from, unsigned long to);
1505 extern int perf_swevent_get_recursion_context(void);
1506 extern void perf_swevent_put_recursion_context(int rctx);
1507 extern u64 perf_swevent_set_period(struct perf_event *event);
1508 extern void perf_event_enable(struct perf_event *event);
1509 extern void perf_event_disable(struct perf_event *event);
1510 extern void perf_event_disable_local(struct perf_event *event);
1511 extern void perf_event_disable_inatomic(struct perf_event *event);
1512 extern void perf_event_task_tick(void);
1513 extern int perf_event_account_interrupt(struct perf_event *event);
1514 extern int perf_event_period(struct perf_event *event, u64 value);
1515 extern u64 perf_event_pause(struct perf_event *event, bool reset);
1516 #else /* !CONFIG_PERF_EVENTS: */
1517 static inline void *
1518 perf_aux_output_begin(struct perf_output_handle *handle,
1519 struct perf_event *event) { return NULL; }
1521 perf_aux_output_end(struct perf_output_handle *handle, unsigned long size)
1524 perf_aux_output_skip(struct perf_output_handle *handle,
1525 unsigned long size) { return -EINVAL; }
1526 static inline void *
1527 perf_get_aux(struct perf_output_handle *handle) { return NULL; }
1529 perf_event_task_migrate(struct task_struct *task) { }
1531 perf_event_task_sched_in(struct task_struct *prev,
1532 struct task_struct *task) { }
1534 perf_event_task_sched_out(struct task_struct *prev,
1535 struct task_struct *next) { }
1536 static inline int perf_event_init_task(struct task_struct *child,
1537 u64 clone_flags) { return 0; }
1538 static inline void perf_event_exit_task(struct task_struct *child) { }
1539 static inline void perf_event_free_task(struct task_struct *task) { }
1540 static inline void perf_event_delayed_put(struct task_struct *task) { }
1541 static inline struct file *perf_event_get(unsigned int fd) { return ERR_PTR(-EINVAL); }
1542 static inline const struct perf_event *perf_get_event(struct file *file)
1544 return ERR_PTR(-EINVAL);
1546 static inline const struct perf_event_attr *perf_event_attrs(struct perf_event *event)
1548 return ERR_PTR(-EINVAL);
1550 static inline int perf_event_read_local(struct perf_event *event, u64 *value,
1551 u64 *enabled, u64 *running)
1555 static inline void perf_event_print_debug(void) { }
1556 static inline int perf_event_task_disable(void) { return -EINVAL; }
1557 static inline int perf_event_task_enable(void) { return -EINVAL; }
1558 static inline int perf_event_refresh(struct perf_event *event, int refresh)
1564 perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr) { }
1566 perf_bp_event(struct perf_event *event, void *data) { }
1568 static inline void perf_event_mmap(struct vm_area_struct *vma) { }
1570 typedef int (perf_ksymbol_get_name_f)(char *name, int name_len, void *data);
1571 static inline void perf_event_ksymbol(u16 ksym_type, u64 addr, u32 len,
1572 bool unregister, const char *sym) { }
1573 static inline void perf_event_bpf_event(struct bpf_prog *prog,
1574 enum perf_bpf_event_type type,
1576 static inline void perf_event_exec(void) { }
1577 static inline void perf_event_comm(struct task_struct *tsk, bool exec) { }
1578 static inline void perf_event_namespaces(struct task_struct *tsk) { }
1579 static inline void perf_event_fork(struct task_struct *tsk) { }
1580 static inline void perf_event_text_poke(const void *addr,
1581 const void *old_bytes,
1583 const void *new_bytes,
1585 static inline void perf_event_init(void) { }
1586 static inline int perf_swevent_get_recursion_context(void) { return -1; }
1587 static inline void perf_swevent_put_recursion_context(int rctx) { }
1588 static inline u64 perf_swevent_set_period(struct perf_event *event) { return 0; }
1589 static inline void perf_event_enable(struct perf_event *event) { }
1590 static inline void perf_event_disable(struct perf_event *event) { }
1591 static inline int __perf_event_disable(void *info) { return -1; }
1592 static inline void perf_event_task_tick(void) { }
1593 static inline int perf_event_release_kernel(struct perf_event *event) { return 0; }
1594 static inline int perf_event_period(struct perf_event *event, u64 value)
1598 static inline u64 perf_event_pause(struct perf_event *event, bool reset)
1604 #if defined(CONFIG_PERF_EVENTS) && defined(CONFIG_CPU_SUP_INTEL)
1605 extern void perf_restore_debug_store(void);
1607 static inline void perf_restore_debug_store(void) { }
1610 static __always_inline bool perf_raw_frag_last(const struct perf_raw_frag *frag)
1612 return frag->pad < sizeof(u64);
1615 #define perf_output_put(handle, x) perf_output_copy((handle), &(x), sizeof(x))
1617 struct perf_pmu_events_attr {
1618 struct device_attribute attr;
1620 const char *event_str;
1623 struct perf_pmu_events_ht_attr {
1624 struct device_attribute attr;
1626 const char *event_str_ht;
1627 const char *event_str_noht;
1630 struct perf_pmu_events_hybrid_attr {
1631 struct device_attribute attr;
1633 const char *event_str;
1637 struct perf_pmu_format_hybrid_attr {
1638 struct device_attribute attr;
1642 ssize_t perf_event_sysfs_show(struct device *dev, struct device_attribute *attr,
1645 #define PMU_EVENT_ATTR(_name, _var, _id, _show) \
1646 static struct perf_pmu_events_attr _var = { \
1647 .attr = __ATTR(_name, 0444, _show, NULL), \
1651 #define PMU_EVENT_ATTR_STRING(_name, _var, _str) \
1652 static struct perf_pmu_events_attr _var = { \
1653 .attr = __ATTR(_name, 0444, perf_event_sysfs_show, NULL), \
1655 .event_str = _str, \
1658 #define PMU_EVENT_ATTR_ID(_name, _show, _id) \
1659 (&((struct perf_pmu_events_attr[]) { \
1660 { .attr = __ATTR(_name, 0444, _show, NULL), \
1664 #define PMU_FORMAT_ATTR(_name, _format) \
1666 _name##_show(struct device *dev, \
1667 struct device_attribute *attr, \
1670 BUILD_BUG_ON(sizeof(_format) >= PAGE_SIZE); \
1671 return sprintf(page, _format "\n"); \
1674 static struct device_attribute format_attr_##_name = __ATTR_RO(_name)
1676 /* Performance counter hotplug functions */
1677 #ifdef CONFIG_PERF_EVENTS
1678 int perf_event_init_cpu(unsigned int cpu);
1679 int perf_event_exit_cpu(unsigned int cpu);
1681 #define perf_event_init_cpu NULL
1682 #define perf_event_exit_cpu NULL
1685 extern void __weak arch_perf_update_userpage(struct perf_event *event,
1686 struct perf_event_mmap_page *userpg,
1690 extern __weak u64 arch_perf_get_page_size(struct mm_struct *mm, unsigned long addr);
1694 * Snapshot branch stack on software events.
1696 * Branch stack can be very useful in understanding software events. For
1697 * example, when a long function, e.g. sys_perf_event_open, returns an
1698 * errno, it is not obvious why the function failed. Branch stack could
1699 * provide very helpful information in this type of scenarios.
1701 * On software event, it is necessary to stop the hardware branch recorder
1702 * fast. Otherwise, the hardware register/buffer will be flushed with
1703 * entries of the triggering event. Therefore, static call is used to
1704 * stop the hardware recorder.
1708 * cnt is the number of entries allocated for entries.
1709 * Return number of entries copied to .
1711 typedef int (perf_snapshot_branch_stack_t)(struct perf_branch_entry *entries,
1713 DECLARE_STATIC_CALL(perf_snapshot_branch_stack, perf_snapshot_branch_stack_t);
1715 #ifndef PERF_NEEDS_LOPWR_CB
1716 static inline void perf_lopwr_cb(bool mode)
1721 #ifdef CONFIG_PERF_EVENTS
1722 static inline bool branch_sample_no_flags(const struct perf_event *event)
1724 return event->attr.branch_sample_type & PERF_SAMPLE_BRANCH_NO_FLAGS;
1727 static inline bool branch_sample_no_cycles(const struct perf_event *event)
1729 return event->attr.branch_sample_type & PERF_SAMPLE_BRANCH_NO_CYCLES;
1732 static inline bool branch_sample_type(const struct perf_event *event)
1734 return event->attr.branch_sample_type & PERF_SAMPLE_BRANCH_TYPE_SAVE;
1737 static inline bool branch_sample_hw_index(const struct perf_event *event)
1739 return event->attr.branch_sample_type & PERF_SAMPLE_BRANCH_HW_INDEX;
1742 static inline bool branch_sample_priv(const struct perf_event *event)
1744 return event->attr.branch_sample_type & PERF_SAMPLE_BRANCH_PRIV_SAVE;
1746 #endif /* CONFIG_PERF_EVENTS */
1747 #endif /* _LINUX_PERF_EVENT_H */