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;
98 static __always_inline bool perf_raw_frag_last(const struct perf_raw_frag *frag)
100 return frag->pad < sizeof(u64);
104 * branch stack layout:
105 * nr: number of taken branches stored in entries[]
106 * hw_idx: The low level index of raw branch records
107 * for the most recent branch.
108 * -1ULL means invalid/unknown.
110 * Note that nr can vary from sample to sample
111 * branches (to, from) are stored from most recent
112 * to least recent, i.e., entries[0] contains the most
114 * The entries[] is an abstraction of raw branch records,
115 * which may not be stored in age order in HW, e.g. Intel LBR.
116 * The hw_idx is to expose the low level index of raw
117 * branch record for the most recent branch aka entries[0].
118 * The hw_idx index is between -1 (unknown) and max depth,
119 * which can be retrieved in /sys/devices/cpu/caps/branches.
120 * For the architectures whose raw branch records are
121 * already stored in age order, the hw_idx should be 0.
123 struct perf_branch_stack {
126 struct perf_branch_entry entries[];
132 * extra PMU register associated with an event
134 struct hw_perf_event_extra {
135 u64 config; /* register value */
136 unsigned int reg; /* register address or index */
137 int alloc; /* extra register already allocated */
138 int idx; /* index in shared_regs->regs[] */
142 * hw_perf_event::flag values
144 * PERF_EVENT_FLAG_ARCH bits are reserved for architecture-specific
147 #define PERF_EVENT_FLAG_ARCH 0x000fffff
148 #define PERF_EVENT_FLAG_USER_READ_CNT 0x80000000
150 static_assert((PERF_EVENT_FLAG_USER_READ_CNT & PERF_EVENT_FLAG_ARCH) == 0);
153 * struct hw_perf_event - performance event hardware details:
155 struct hw_perf_event {
156 #ifdef CONFIG_PERF_EVENTS
158 struct { /* hardware */
161 unsigned long config_base;
162 unsigned long event_base;
163 int event_base_rdpmc;
168 struct hw_perf_event_extra extra_reg;
169 struct hw_perf_event_extra branch_reg;
171 struct { /* software */
172 struct hrtimer hrtimer;
174 struct { /* tracepoint */
175 /* for tp_event->class */
176 struct list_head tp_list;
178 struct { /* amd_power */
182 #ifdef CONFIG_HAVE_HW_BREAKPOINT
183 struct { /* breakpoint */
185 * Crufty hack to avoid the chicken and egg
186 * problem hw_breakpoint has with context
187 * creation and event initalization.
189 struct arch_hw_breakpoint info;
190 struct rhlist_head bp_list;
193 struct { /* amd_iommu */
202 * If the event is a per task event, this will point to the task in
203 * question. See the comment in perf_event_alloc().
205 struct task_struct *target;
208 * PMU would store hardware filter configuration
213 /* Last sync'ed generation of filters */
214 unsigned long addr_filters_gen;
217 * hw_perf_event::state flags; used to track the PERF_EF_* state.
219 #define PERF_HES_STOPPED 0x01 /* the counter is stopped */
220 #define PERF_HES_UPTODATE 0x02 /* event->count up-to-date */
221 #define PERF_HES_ARCH 0x04
226 * The last observed hardware counter value, updated with a
227 * local64_cmpxchg() such that pmu::read() can be called nested.
229 local64_t prev_count;
232 * The period to start the next sample with.
237 struct { /* Sampling */
239 * The period we started this sample with.
244 * However much is left of the current period;
245 * note that this is a full 64bit value and
246 * allows for generation of periods longer
247 * than hardware might allow.
249 local64_t period_left;
251 struct { /* Topdown events counting for context switch */
258 * State for throttling the event, see __perf_event_overflow() and
259 * perf_adjust_freq_unthr_context().
265 * State for freq target events, see __perf_event_overflow() and
266 * perf_adjust_freq_unthr_context().
269 u64 freq_count_stamp;
274 struct perf_event_pmu_context;
277 * Common implementation detail of pmu::{start,commit,cancel}_txn
279 #define PERF_PMU_TXN_ADD 0x1 /* txn to add/schedule event on PMU */
280 #define PERF_PMU_TXN_READ 0x2 /* txn to read event group from PMU */
283 * pmu::capabilities flags
285 #define PERF_PMU_CAP_NO_INTERRUPT 0x0001
286 #define PERF_PMU_CAP_NO_NMI 0x0002
287 #define PERF_PMU_CAP_AUX_NO_SG 0x0004
288 #define PERF_PMU_CAP_EXTENDED_REGS 0x0008
289 #define PERF_PMU_CAP_EXCLUSIVE 0x0010
290 #define PERF_PMU_CAP_ITRACE 0x0020
291 #define PERF_PMU_CAP_NO_EXCLUDE 0x0040
292 #define PERF_PMU_CAP_AUX_OUTPUT 0x0080
293 #define PERF_PMU_CAP_EXTENDED_HW_TYPE 0x0100
295 struct perf_output_handle;
297 #define PMU_NULL_DEV ((void *)(~0UL))
300 * struct pmu - generic performance monitoring unit
303 struct list_head entry;
305 struct module *module;
307 struct device *parent;
308 const struct attribute_group **attr_groups;
309 const struct attribute_group **attr_update;
314 * various common per-pmu feature flags
318 int __percpu *pmu_disable_count;
319 struct perf_cpu_pmu_context __percpu *cpu_pmu_context;
320 atomic_t exclusive_cnt; /* < 0: cpu; > 0: tsk */
322 int hrtimer_interval_ms;
324 /* number of address filters this PMU can do */
325 unsigned int nr_addr_filters;
328 * Fully disable/enable this PMU, can be used to protect from the PMI
329 * as well as for lazy/batch writing of the MSRs.
331 void (*pmu_enable) (struct pmu *pmu); /* optional */
332 void (*pmu_disable) (struct pmu *pmu); /* optional */
335 * Try and initialize the event for this PMU.
338 * -ENOENT -- @event is not for this PMU
340 * -ENODEV -- @event is for this PMU but PMU not present
341 * -EBUSY -- @event is for this PMU but PMU temporarily unavailable
342 * -EINVAL -- @event is for this PMU but @event is not valid
343 * -EOPNOTSUPP -- @event is for this PMU, @event is valid, but not supported
344 * -EACCES -- @event is for this PMU, @event is valid, but no privileges
346 * 0 -- @event is for this PMU and valid
348 * Other error return values are allowed.
350 int (*event_init) (struct perf_event *event);
353 * Notification that the event was mapped or unmapped. Called
354 * in the context of the mapping task.
356 void (*event_mapped) (struct perf_event *event, struct mm_struct *mm); /* optional */
357 void (*event_unmapped) (struct perf_event *event, struct mm_struct *mm); /* optional */
360 * Flags for ->add()/->del()/ ->start()/->stop(). There are
361 * matching hw_perf_event::state flags.
363 #define PERF_EF_START 0x01 /* start the counter when adding */
364 #define PERF_EF_RELOAD 0x02 /* reload the counter when starting */
365 #define PERF_EF_UPDATE 0x04 /* update the counter when stopping */
368 * Adds/Removes a counter to/from the PMU, can be done inside a
369 * transaction, see the ->*_txn() methods.
371 * The add/del callbacks will reserve all hardware resources required
372 * to service the event, this includes any counter constraint
375 * Called with IRQs disabled and the PMU disabled on the CPU the event
378 * ->add() called without PERF_EF_START should result in the same state
379 * as ->add() followed by ->stop().
381 * ->del() must always PERF_EF_UPDATE stop an event. If it calls
382 * ->stop() that must deal with already being stopped without
385 int (*add) (struct perf_event *event, int flags);
386 void (*del) (struct perf_event *event, int flags);
389 * Starts/Stops a counter present on the PMU.
391 * The PMI handler should stop the counter when perf_event_overflow()
392 * returns !0. ->start() will be used to continue.
394 * Also used to change the sample period.
396 * Called with IRQs disabled and the PMU disabled on the CPU the event
397 * is on -- will be called from NMI context with the PMU generates
400 * ->stop() with PERF_EF_UPDATE will read the counter and update
401 * period/count values like ->read() would.
403 * ->start() with PERF_EF_RELOAD will reprogram the counter
404 * value, must be preceded by a ->stop() with PERF_EF_UPDATE.
406 void (*start) (struct perf_event *event, int flags);
407 void (*stop) (struct perf_event *event, int flags);
410 * Updates the counter value of the event.
412 * For sampling capable PMUs this will also update the software period
413 * hw_perf_event::period_left field.
415 void (*read) (struct perf_event *event);
418 * Group events scheduling is treated as a transaction, add
419 * group events as a whole and perform one schedulability test.
420 * If the test fails, roll back the whole group
422 * Start the transaction, after this ->add() doesn't need to
423 * do schedulability tests.
427 void (*start_txn) (struct pmu *pmu, unsigned int txn_flags);
429 * If ->start_txn() disabled the ->add() schedulability test
430 * then ->commit_txn() is required to perform one. On success
431 * the transaction is closed. On error the transaction is kept
432 * open until ->cancel_txn() is called.
436 int (*commit_txn) (struct pmu *pmu);
438 * Will cancel the transaction, assumes ->del() is called
439 * for each successful ->add() during the transaction.
443 void (*cancel_txn) (struct pmu *pmu);
446 * Will return the value for perf_event_mmap_page::index for this event,
447 * if no implementation is provided it will default to 0 (see
448 * perf_event_idx_default).
450 int (*event_idx) (struct perf_event *event); /*optional */
453 * context-switches callback
455 void (*sched_task) (struct perf_event_pmu_context *pmu_ctx,
459 * Kmem cache of PMU specific data
461 struct kmem_cache *task_ctx_cache;
464 * PMU specific parts of task perf event context (i.e. ctx->task_ctx_data)
465 * can be synchronized using this function. See Intel LBR callstack support
466 * implementation and Perf core context switch handling callbacks for usage
469 void (*swap_task_ctx) (struct perf_event_pmu_context *prev_epc,
470 struct perf_event_pmu_context *next_epc);
474 * Set up pmu-private data structures for an AUX area
476 void *(*setup_aux) (struct perf_event *event, void **pages,
477 int nr_pages, bool overwrite);
481 * Free pmu-private AUX data structures
483 void (*free_aux) (void *aux); /* optional */
486 * Take a snapshot of the AUX buffer without touching the event
487 * state, so that preempting ->start()/->stop() callbacks does
488 * not interfere with their logic. Called in PMI context.
490 * Returns the size of AUX data copied to the output handle.
494 long (*snapshot_aux) (struct perf_event *event,
495 struct perf_output_handle *handle,
499 * Validate address range filters: make sure the HW supports the
500 * requested configuration and number of filters; return 0 if the
501 * supplied filters are valid, -errno otherwise.
503 * Runs in the context of the ioctl()ing process and is not serialized
504 * with the rest of the PMU callbacks.
506 int (*addr_filters_validate) (struct list_head *filters);
510 * Synchronize address range filter configuration:
511 * translate hw-agnostic filters into hardware configuration in
512 * event::hw::addr_filters.
514 * Runs as a part of filter sync sequence that is done in ->start()
515 * callback by calling perf_event_addr_filters_sync().
517 * May (and should) traverse event::addr_filters::list, for which its
518 * caller provides necessary serialization.
520 void (*addr_filters_sync) (struct perf_event *event);
524 * Check if event can be used for aux_output purposes for
525 * events of this PMU.
527 * Runs from perf_event_open(). Should return 0 for "no match"
528 * or non-zero for "match".
530 int (*aux_output_match) (struct perf_event *event);
534 * Skip programming this PMU on the given CPU. Typically needed for
537 bool (*filter) (struct pmu *pmu, int cpu); /* optional */
540 * Check period value for PERF_EVENT_IOC_PERIOD ioctl.
542 int (*check_period) (struct perf_event *event, u64 value); /* optional */
545 enum perf_addr_filter_action_t {
546 PERF_ADDR_FILTER_ACTION_STOP = 0,
547 PERF_ADDR_FILTER_ACTION_START,
548 PERF_ADDR_FILTER_ACTION_FILTER,
552 * struct perf_addr_filter - address range filter definition
553 * @entry: event's filter list linkage
554 * @path: object file's path for file-based filters
555 * @offset: filter range offset
556 * @size: filter range size (size==0 means single address trigger)
557 * @action: filter/start/stop
559 * This is a hardware-agnostic filter configuration as specified by the user.
561 struct perf_addr_filter {
562 struct list_head entry;
564 unsigned long offset;
566 enum perf_addr_filter_action_t action;
570 * struct perf_addr_filters_head - container for address range filters
571 * @list: list of filters for this event
572 * @lock: spinlock that serializes accesses to the @list and event's
573 * (and its children's) filter generations.
574 * @nr_file_filters: number of file-based filters
576 * A child event will use parent's @list (and therefore @lock), so they are
577 * bundled together; see perf_event_addr_filters().
579 struct perf_addr_filters_head {
580 struct list_head list;
582 unsigned int nr_file_filters;
585 struct perf_addr_filter_range {
591 * enum perf_event_state - the states of an event:
593 enum perf_event_state {
594 PERF_EVENT_STATE_DEAD = -4,
595 PERF_EVENT_STATE_EXIT = -3,
596 PERF_EVENT_STATE_ERROR = -2,
597 PERF_EVENT_STATE_OFF = -1,
598 PERF_EVENT_STATE_INACTIVE = 0,
599 PERF_EVENT_STATE_ACTIVE = 1,
603 struct perf_sample_data;
605 typedef void (*perf_overflow_handler_t)(struct perf_event *,
606 struct perf_sample_data *,
607 struct pt_regs *regs);
610 * Event capabilities. For event_caps and groups caps.
612 * PERF_EV_CAP_SOFTWARE: Is a software event.
613 * PERF_EV_CAP_READ_ACTIVE_PKG: A CPU event (or cgroup event) that can be read
614 * from any CPU in the package where it is active.
615 * PERF_EV_CAP_SIBLING: An event with this flag must be a group sibling and
616 * cannot be a group leader. If an event with this flag is detached from the
617 * group it is scheduled out and moved into an unrecoverable ERROR state.
619 #define PERF_EV_CAP_SOFTWARE BIT(0)
620 #define PERF_EV_CAP_READ_ACTIVE_PKG BIT(1)
621 #define PERF_EV_CAP_SIBLING BIT(2)
623 #define SWEVENT_HLIST_BITS 8
624 #define SWEVENT_HLIST_SIZE (1 << SWEVENT_HLIST_BITS)
626 struct swevent_hlist {
627 struct hlist_head heads[SWEVENT_HLIST_SIZE];
628 struct rcu_head rcu_head;
631 #define PERF_ATTACH_CONTEXT 0x01
632 #define PERF_ATTACH_GROUP 0x02
633 #define PERF_ATTACH_TASK 0x04
634 #define PERF_ATTACH_TASK_DATA 0x08
635 #define PERF_ATTACH_ITRACE 0x10
636 #define PERF_ATTACH_SCHED_CB 0x20
637 #define PERF_ATTACH_CHILD 0x40
643 struct pmu_event_list {
645 struct list_head list;
649 * event->sibling_list is modified whole holding both ctx->lock and ctx->mutex
650 * as such iteration must hold either lock. However, since ctx->lock is an IRQ
651 * safe lock, and is only held by the CPU doing the modification, having IRQs
652 * disabled is sufficient since it will hold-off the IPIs.
654 #ifdef CONFIG_PROVE_LOCKING
655 #define lockdep_assert_event_ctx(event) \
656 WARN_ON_ONCE(__lockdep_enabled && \
657 (this_cpu_read(hardirqs_enabled) && \
658 lockdep_is_held(&(event)->ctx->mutex) != LOCK_STATE_HELD))
660 #define lockdep_assert_event_ctx(event)
663 #define for_each_sibling_event(sibling, event) \
664 lockdep_assert_event_ctx(event); \
665 if ((event)->group_leader == (event)) \
666 list_for_each_entry((sibling), &(event)->sibling_list, sibling_list)
669 * struct perf_event - performance event kernel representation:
672 #ifdef CONFIG_PERF_EVENTS
674 * entry onto perf_event_context::event_list;
675 * modifications require ctx->lock
676 * RCU safe iterations.
678 struct list_head event_entry;
681 * Locked for modification by both ctx->mutex and ctx->lock; holding
682 * either sufficies for read.
684 struct list_head sibling_list;
685 struct list_head active_list;
687 * Node on the pinned or flexible tree located at the event context;
689 struct rb_node group_node;
692 * We need storage to track the entries in perf_pmu_migrate_context; we
693 * cannot use the event_entry because of RCU and we want to keep the
694 * group in tact which avoids us using the other two entries.
696 struct list_head migrate_entry;
698 struct hlist_node hlist_entry;
699 struct list_head active_entry;
702 /* Not serialized. Only written during event initialization. */
704 /* The cumulative AND of all event_caps for events in this group. */
707 struct perf_event *group_leader;
709 * event->pmu will always point to pmu in which this event belongs.
710 * Whereas event->pmu_ctx->pmu may point to other pmu when group of
711 * different pmu events is created.
716 enum perf_event_state state;
717 unsigned int attach_state;
719 atomic64_t child_count;
722 * These are the total time in nanoseconds that the event
723 * has been enabled (i.e. eligible to run, and the task has
724 * been scheduled in, if this is a per-task event)
725 * and running (scheduled onto the CPU), respectively.
727 u64 total_time_enabled;
728 u64 total_time_running;
731 struct perf_event_attr attr;
735 struct hw_perf_event hw;
737 struct perf_event_context *ctx;
739 * event->pmu_ctx points to perf_event_pmu_context in which the event
740 * is added. This pmu_ctx can be of other pmu for sw event when that
741 * sw event is part of a group which also contains non-sw events.
743 struct perf_event_pmu_context *pmu_ctx;
744 atomic_long_t refcount;
747 * These accumulate total time (in nanoseconds) that children
748 * events have been enabled and running, respectively.
750 atomic64_t child_total_time_enabled;
751 atomic64_t child_total_time_running;
754 * Protect attach/detach and child_list:
756 struct mutex child_mutex;
757 struct list_head child_list;
758 struct perf_event *parent;
763 struct list_head owner_entry;
764 struct task_struct *owner;
767 struct mutex mmap_mutex;
770 struct perf_buffer *rb;
771 struct list_head rb_entry;
772 unsigned long rcu_batches;
776 wait_queue_head_t waitq;
777 struct fasync_struct *fasync;
779 /* delayed work for NMIs and such */
780 unsigned int pending_wakeup;
781 unsigned int pending_kill;
782 unsigned int pending_disable;
783 unsigned int pending_sigtrap;
784 unsigned long pending_addr; /* SIGTRAP */
785 struct irq_work pending_irq;
786 struct callback_head pending_task;
787 unsigned int pending_work;
789 atomic_t event_limit;
791 /* address range filters */
792 struct perf_addr_filters_head addr_filters;
793 /* vma address array for file-based filders */
794 struct perf_addr_filter_range *addr_filter_ranges;
795 unsigned long addr_filters_gen;
797 /* for aux_output events */
798 struct perf_event *aux_event;
800 void (*destroy)(struct perf_event *);
801 struct rcu_head rcu_head;
803 struct pid_namespace *ns;
806 atomic64_t lost_samples;
809 perf_overflow_handler_t overflow_handler;
810 void *overflow_handler_context;
811 #ifdef CONFIG_BPF_SYSCALL
812 perf_overflow_handler_t orig_overflow_handler;
813 struct bpf_prog *prog;
817 #ifdef CONFIG_EVENT_TRACING
818 struct trace_event_call *tp_event;
819 struct event_filter *filter;
820 #ifdef CONFIG_FUNCTION_TRACER
821 struct ftrace_ops ftrace_ops;
825 #ifdef CONFIG_CGROUP_PERF
826 struct perf_cgroup *cgrp; /* cgroup event is attach to */
829 #ifdef CONFIG_SECURITY
832 struct list_head sb_list;
835 * Certain events gets forwarded to another pmu internally by over-
836 * writing kernel copy of event->attr.type without user being aware
837 * of it. event->orig_type contains original 'type' requested by
841 #endif /* CONFIG_PERF_EVENTS */
845 * ,-----------------------[1:n]----------------------.
847 * perf_event_context <-[1:n]-> perf_event_pmu_context <--- perf_event
849 * `--------[1:n]---------' `-[n:1]-> pmu <-[1:n]-'
852 * struct perf_event_pmu_context lifetime is refcount based and RCU freed
853 * (similar to perf_event_context). Locking is as if it were a member of
854 * perf_event_context; specifically:
856 * modification, both: ctx->mutex && ctx->lock
857 * reading, either: ctx->mutex || ctx->lock
859 * There is one exception to this; namely put_pmu_ctx() isn't always called
860 * with ctx->mutex held; this means that as long as we can guarantee the epc
861 * has events the above rules hold.
863 * Specificially, sys_perf_event_open()'s group_leader case depends on
864 * ctx->mutex pinning the configuration. Since we hold a reference on
865 * group_leader (through the filedesc) it can't go away, therefore it's
866 * associated pmu_ctx must exist and cannot change due to ctx->mutex.
868 struct perf_event_pmu_context {
870 struct perf_event_context *ctx;
872 struct list_head pmu_ctx_entry;
874 struct list_head pinned_active;
875 struct list_head flexible_active;
877 /* Used to avoid freeing per-cpu perf_event_pmu_context */
878 unsigned int embedded : 1;
880 unsigned int nr_events;
882 atomic_t refcount; /* event <-> epc */
883 struct rcu_head rcu_head;
885 void *task_ctx_data; /* pmu specific data */
887 * Set when one or more (plausibly active) event can't be scheduled
888 * due to pmu overcommit or pmu constraints, except tolerant to
889 * events not necessary to be active due to scheduling constraints,
892 int rotate_necessary;
895 struct perf_event_groups {
902 * struct perf_event_context - event context structure
904 * Used as a container for task events and CPU events as well:
906 struct perf_event_context {
908 * Protect the states of the events in the list,
909 * nr_active, and the list:
913 * Protect the list of events. Locking either mutex or lock
914 * is sufficient to ensure the list doesn't change; to change
915 * the list you need to lock both the mutex and the spinlock.
919 struct list_head pmu_ctx_list;
920 struct perf_event_groups pinned_groups;
921 struct perf_event_groups flexible_groups;
922 struct list_head event_list;
933 refcount_t refcount; /* event <-> ctx */
934 struct task_struct *task;
937 * Context clock, runs when context enabled.
944 * These fields let us detect when two contexts have both
945 * been cloned (inherited) from a common ancestor.
947 struct perf_event_context *parent_ctx;
951 #ifdef CONFIG_CGROUP_PERF
952 int nr_cgroups; /* cgroup evts */
954 struct rcu_head rcu_head;
957 * Sum (event->pending_sigtrap + event->pending_work)
959 * The SIGTRAP is targeted at ctx->task, as such it won't do changing
960 * that until the signal is delivered.
966 * Number of contexts where an event can trigger:
967 * task, softirq, hardirq, nmi.
969 #define PERF_NR_CONTEXTS 4
971 struct perf_cpu_pmu_context {
972 struct perf_event_pmu_context epc;
973 struct perf_event_pmu_context *task_epc;
975 struct list_head sched_cb_entry;
981 raw_spinlock_t hrtimer_lock;
982 struct hrtimer hrtimer;
983 ktime_t hrtimer_interval;
984 unsigned int hrtimer_active;
988 * struct perf_event_cpu_context - per cpu event context structure
990 struct perf_cpu_context {
991 struct perf_event_context ctx;
992 struct perf_event_context *task_ctx;
995 #ifdef CONFIG_CGROUP_PERF
996 struct perf_cgroup *cgrp;
1000 * Per-CPU storage for iterators used in visit_groups_merge. The default
1001 * storage is of size 2 to hold the CPU and any CPU event iterators.
1004 struct perf_event **heap;
1005 struct perf_event *heap_default[2];
1008 struct perf_output_handle {
1009 struct perf_event *event;
1010 struct perf_buffer *rb;
1011 unsigned long wakeup;
1021 struct bpf_perf_event_data_kern {
1022 bpf_user_pt_regs_t *regs;
1023 struct perf_sample_data *data;
1024 struct perf_event *event;
1027 #ifdef CONFIG_CGROUP_PERF
1030 * perf_cgroup_info keeps track of time_enabled for a cgroup.
1031 * This is a per-cpu dynamically allocated data structure.
1033 struct perf_cgroup_info {
1040 struct perf_cgroup {
1041 struct cgroup_subsys_state css;
1042 struct perf_cgroup_info __percpu *info;
1046 * Must ensure cgroup is pinned (css_get) before calling
1047 * this function. In other words, we cannot call this function
1048 * if there is no cgroup event for the current CPU context.
1050 static inline struct perf_cgroup *
1051 perf_cgroup_from_task(struct task_struct *task, struct perf_event_context *ctx)
1053 return container_of(task_css_check(task, perf_event_cgrp_id,
1054 ctx ? lockdep_is_held(&ctx->lock)
1056 struct perf_cgroup, css);
1058 #endif /* CONFIG_CGROUP_PERF */
1060 #ifdef CONFIG_PERF_EVENTS
1062 extern struct perf_event_context *perf_cpu_task_ctx(void);
1064 extern void *perf_aux_output_begin(struct perf_output_handle *handle,
1065 struct perf_event *event);
1066 extern void perf_aux_output_end(struct perf_output_handle *handle,
1067 unsigned long size);
1068 extern int perf_aux_output_skip(struct perf_output_handle *handle,
1069 unsigned long size);
1070 extern void *perf_get_aux(struct perf_output_handle *handle);
1071 extern void perf_aux_output_flag(struct perf_output_handle *handle, u64 flags);
1072 extern void perf_event_itrace_started(struct perf_event *event);
1074 extern int perf_pmu_register(struct pmu *pmu, const char *name, int type);
1075 extern void perf_pmu_unregister(struct pmu *pmu);
1077 extern void __perf_event_task_sched_in(struct task_struct *prev,
1078 struct task_struct *task);
1079 extern void __perf_event_task_sched_out(struct task_struct *prev,
1080 struct task_struct *next);
1081 extern int perf_event_init_task(struct task_struct *child, u64 clone_flags);
1082 extern void perf_event_exit_task(struct task_struct *child);
1083 extern void perf_event_free_task(struct task_struct *task);
1084 extern void perf_event_delayed_put(struct task_struct *task);
1085 extern struct file *perf_event_get(unsigned int fd);
1086 extern const struct perf_event *perf_get_event(struct file *file);
1087 extern const struct perf_event_attr *perf_event_attrs(struct perf_event *event);
1088 extern void perf_event_print_debug(void);
1089 extern void perf_pmu_disable(struct pmu *pmu);
1090 extern void perf_pmu_enable(struct pmu *pmu);
1091 extern void perf_sched_cb_dec(struct pmu *pmu);
1092 extern void perf_sched_cb_inc(struct pmu *pmu);
1093 extern int perf_event_task_disable(void);
1094 extern int perf_event_task_enable(void);
1096 extern void perf_pmu_resched(struct pmu *pmu);
1098 extern int perf_event_refresh(struct perf_event *event, int refresh);
1099 extern void perf_event_update_userpage(struct perf_event *event);
1100 extern int perf_event_release_kernel(struct perf_event *event);
1101 extern struct perf_event *
1102 perf_event_create_kernel_counter(struct perf_event_attr *attr,
1104 struct task_struct *task,
1105 perf_overflow_handler_t callback,
1107 extern void perf_pmu_migrate_context(struct pmu *pmu,
1108 int src_cpu, int dst_cpu);
1109 int perf_event_read_local(struct perf_event *event, u64 *value,
1110 u64 *enabled, u64 *running);
1111 extern u64 perf_event_read_value(struct perf_event *event,
1112 u64 *enabled, u64 *running);
1114 extern struct perf_callchain_entry *perf_callchain(struct perf_event *event, struct pt_regs *regs);
1116 static inline bool branch_sample_no_flags(const struct perf_event *event)
1118 return event->attr.branch_sample_type & PERF_SAMPLE_BRANCH_NO_FLAGS;
1121 static inline bool branch_sample_no_cycles(const struct perf_event *event)
1123 return event->attr.branch_sample_type & PERF_SAMPLE_BRANCH_NO_CYCLES;
1126 static inline bool branch_sample_type(const struct perf_event *event)
1128 return event->attr.branch_sample_type & PERF_SAMPLE_BRANCH_TYPE_SAVE;
1131 static inline bool branch_sample_hw_index(const struct perf_event *event)
1133 return event->attr.branch_sample_type & PERF_SAMPLE_BRANCH_HW_INDEX;
1136 static inline bool branch_sample_priv(const struct perf_event *event)
1138 return event->attr.branch_sample_type & PERF_SAMPLE_BRANCH_PRIV_SAVE;
1142 struct perf_sample_data {
1144 * Fields set by perf_sample_data_init() unconditionally,
1145 * group so as to minimize the cachelines touched.
1152 * Fields commonly set by __perf_event_header__init_id(),
1153 * group so as to minimize the cachelines touched.
1168 * The other fields, optionally {set,used} by
1169 * perf_{prepare,output}_sample().
1172 struct perf_callchain_entry *callchain;
1173 struct perf_raw_record *raw;
1174 struct perf_branch_stack *br_stack;
1175 union perf_sample_weight weight;
1176 union perf_mem_data_src data_src;
1179 struct perf_regs regs_user;
1180 struct perf_regs regs_intr;
1181 u64 stack_user_size;
1190 } ____cacheline_aligned;
1192 /* default value for data source */
1193 #define PERF_MEM_NA (PERF_MEM_S(OP, NA) |\
1194 PERF_MEM_S(LVL, NA) |\
1195 PERF_MEM_S(SNOOP, NA) |\
1196 PERF_MEM_S(LOCK, NA) |\
1197 PERF_MEM_S(TLB, NA) |\
1198 PERF_MEM_S(LVLNUM, NA))
1200 static inline void perf_sample_data_init(struct perf_sample_data *data,
1201 u64 addr, u64 period)
1203 /* remaining struct members initialized in perf_prepare_sample() */
1204 data->sample_flags = PERF_SAMPLE_PERIOD;
1205 data->period = period;
1210 data->sample_flags |= PERF_SAMPLE_ADDR;
1214 static inline void perf_sample_save_callchain(struct perf_sample_data *data,
1215 struct perf_event *event,
1216 struct pt_regs *regs)
1220 data->callchain = perf_callchain(event, regs);
1221 size += data->callchain->nr;
1223 data->dyn_size += size * sizeof(u64);
1224 data->sample_flags |= PERF_SAMPLE_CALLCHAIN;
1227 static inline void perf_sample_save_raw_data(struct perf_sample_data *data,
1228 struct perf_raw_record *raw)
1230 struct perf_raw_frag *frag = &raw->frag;
1236 if (perf_raw_frag_last(frag))
1241 size = round_up(sum + sizeof(u32), sizeof(u64));
1242 raw->size = size - sizeof(u32);
1243 frag->pad = raw->size - sum;
1246 data->dyn_size += size;
1247 data->sample_flags |= PERF_SAMPLE_RAW;
1250 static inline void perf_sample_save_brstack(struct perf_sample_data *data,
1251 struct perf_event *event,
1252 struct perf_branch_stack *brs)
1254 int size = sizeof(u64); /* nr */
1256 if (branch_sample_hw_index(event))
1257 size += sizeof(u64);
1258 size += brs->nr * sizeof(struct perf_branch_entry);
1260 data->br_stack = brs;
1261 data->dyn_size += size;
1262 data->sample_flags |= PERF_SAMPLE_BRANCH_STACK;
1265 static inline u32 perf_sample_data_size(struct perf_sample_data *data,
1266 struct perf_event *event)
1268 u32 size = sizeof(struct perf_event_header);
1270 size += event->header_size + event->id_header_size;
1271 size += data->dyn_size;
1277 * Clear all bitfields in the perf_branch_entry.
1278 * The to and from fields are not cleared because they are
1279 * systematically modified by caller.
1281 static inline void perf_clear_branch_entry_bitfields(struct perf_branch_entry *br)
1289 br->spec = PERF_BR_SPEC_NA;
1293 extern void perf_output_sample(struct perf_output_handle *handle,
1294 struct perf_event_header *header,
1295 struct perf_sample_data *data,
1296 struct perf_event *event);
1297 extern void perf_prepare_sample(struct perf_sample_data *data,
1298 struct perf_event *event,
1299 struct pt_regs *regs);
1300 extern void perf_prepare_header(struct perf_event_header *header,
1301 struct perf_sample_data *data,
1302 struct perf_event *event,
1303 struct pt_regs *regs);
1305 extern int perf_event_overflow(struct perf_event *event,
1306 struct perf_sample_data *data,
1307 struct pt_regs *regs);
1309 extern void perf_event_output_forward(struct perf_event *event,
1310 struct perf_sample_data *data,
1311 struct pt_regs *regs);
1312 extern void perf_event_output_backward(struct perf_event *event,
1313 struct perf_sample_data *data,
1314 struct pt_regs *regs);
1315 extern int perf_event_output(struct perf_event *event,
1316 struct perf_sample_data *data,
1317 struct pt_regs *regs);
1320 __is_default_overflow_handler(perf_overflow_handler_t overflow_handler)
1322 if (likely(overflow_handler == perf_event_output_forward))
1324 if (unlikely(overflow_handler == perf_event_output_backward))
1329 #define is_default_overflow_handler(event) \
1330 __is_default_overflow_handler((event)->overflow_handler)
1332 #ifdef CONFIG_BPF_SYSCALL
1333 static inline bool uses_default_overflow_handler(struct perf_event *event)
1335 if (likely(is_default_overflow_handler(event)))
1338 return __is_default_overflow_handler(event->orig_overflow_handler);
1341 #define uses_default_overflow_handler(event) \
1342 is_default_overflow_handler(event)
1346 perf_event_header__init_id(struct perf_event_header *header,
1347 struct perf_sample_data *data,
1348 struct perf_event *event);
1350 perf_event__output_id_sample(struct perf_event *event,
1351 struct perf_output_handle *handle,
1352 struct perf_sample_data *sample);
1355 perf_log_lost_samples(struct perf_event *event, u64 lost);
1357 static inline bool event_has_any_exclude_flag(struct perf_event *event)
1359 struct perf_event_attr *attr = &event->attr;
1361 return attr->exclude_idle || attr->exclude_user ||
1362 attr->exclude_kernel || attr->exclude_hv ||
1363 attr->exclude_guest || attr->exclude_host;
1366 static inline bool is_sampling_event(struct perf_event *event)
1368 return event->attr.sample_period != 0;
1372 * Return 1 for a software event, 0 for a hardware event
1374 static inline int is_software_event(struct perf_event *event)
1376 return event->event_caps & PERF_EV_CAP_SOFTWARE;
1380 * Return 1 for event in sw context, 0 for event in hw context
1382 static inline int in_software_context(struct perf_event *event)
1384 return event->pmu_ctx->pmu->task_ctx_nr == perf_sw_context;
1387 static inline int is_exclusive_pmu(struct pmu *pmu)
1389 return pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE;
1392 extern struct static_key perf_swevent_enabled[PERF_COUNT_SW_MAX];
1394 extern void ___perf_sw_event(u32, u64, struct pt_regs *, u64);
1395 extern void __perf_sw_event(u32, u64, struct pt_regs *, u64);
1397 #ifndef perf_arch_fetch_caller_regs
1398 static inline void perf_arch_fetch_caller_regs(struct pt_regs *regs, unsigned long ip) { }
1402 * When generating a perf sample in-line, instead of from an interrupt /
1403 * exception, we lack a pt_regs. This is typically used from software events
1404 * like: SW_CONTEXT_SWITCHES, SW_MIGRATIONS and the tie-in with tracepoints.
1406 * We typically don't need a full set, but (for x86) do require:
1407 * - ip for PERF_SAMPLE_IP
1408 * - cs for user_mode() tests
1409 * - sp for PERF_SAMPLE_CALLCHAIN
1410 * - eflags for MISC bits and CALLCHAIN (see: perf_hw_regs())
1412 * NOTE: assumes @regs is otherwise already 0 filled; this is important for
1413 * things like PERF_SAMPLE_REGS_INTR.
1415 static inline void perf_fetch_caller_regs(struct pt_regs *regs)
1417 perf_arch_fetch_caller_regs(regs, CALLER_ADDR0);
1420 static __always_inline void
1421 perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr)
1423 if (static_key_false(&perf_swevent_enabled[event_id]))
1424 __perf_sw_event(event_id, nr, regs, addr);
1427 DECLARE_PER_CPU(struct pt_regs, __perf_regs[4]);
1430 * 'Special' version for the scheduler, it hard assumes no recursion,
1431 * which is guaranteed by us not actually scheduling inside other swevents
1432 * because those disable preemption.
1434 static __always_inline void __perf_sw_event_sched(u32 event_id, u64 nr, u64 addr)
1436 struct pt_regs *regs = this_cpu_ptr(&__perf_regs[0]);
1438 perf_fetch_caller_regs(regs);
1439 ___perf_sw_event(event_id, nr, regs, addr);
1442 extern struct static_key_false perf_sched_events;
1444 static __always_inline bool __perf_sw_enabled(int swevt)
1446 return static_key_false(&perf_swevent_enabled[swevt]);
1449 static inline void perf_event_task_migrate(struct task_struct *task)
1451 if (__perf_sw_enabled(PERF_COUNT_SW_CPU_MIGRATIONS))
1452 task->sched_migrated = 1;
1455 static inline void perf_event_task_sched_in(struct task_struct *prev,
1456 struct task_struct *task)
1458 if (static_branch_unlikely(&perf_sched_events))
1459 __perf_event_task_sched_in(prev, task);
1461 if (__perf_sw_enabled(PERF_COUNT_SW_CPU_MIGRATIONS) &&
1462 task->sched_migrated) {
1463 __perf_sw_event_sched(PERF_COUNT_SW_CPU_MIGRATIONS, 1, 0);
1464 task->sched_migrated = 0;
1468 static inline void perf_event_task_sched_out(struct task_struct *prev,
1469 struct task_struct *next)
1471 if (__perf_sw_enabled(PERF_COUNT_SW_CONTEXT_SWITCHES))
1472 __perf_sw_event_sched(PERF_COUNT_SW_CONTEXT_SWITCHES, 1, 0);
1474 #ifdef CONFIG_CGROUP_PERF
1475 if (__perf_sw_enabled(PERF_COUNT_SW_CGROUP_SWITCHES) &&
1476 perf_cgroup_from_task(prev, NULL) !=
1477 perf_cgroup_from_task(next, NULL))
1478 __perf_sw_event_sched(PERF_COUNT_SW_CGROUP_SWITCHES, 1, 0);
1481 if (static_branch_unlikely(&perf_sched_events))
1482 __perf_event_task_sched_out(prev, next);
1485 extern void perf_event_mmap(struct vm_area_struct *vma);
1487 extern void perf_event_ksymbol(u16 ksym_type, u64 addr, u32 len,
1488 bool unregister, const char *sym);
1489 extern void perf_event_bpf_event(struct bpf_prog *prog,
1490 enum perf_bpf_event_type type,
1493 #ifdef CONFIG_GUEST_PERF_EVENTS
1494 extern struct perf_guest_info_callbacks __rcu *perf_guest_cbs;
1496 DECLARE_STATIC_CALL(__perf_guest_state, *perf_guest_cbs->state);
1497 DECLARE_STATIC_CALL(__perf_guest_get_ip, *perf_guest_cbs->get_ip);
1498 DECLARE_STATIC_CALL(__perf_guest_handle_intel_pt_intr, *perf_guest_cbs->handle_intel_pt_intr);
1500 static inline unsigned int perf_guest_state(void)
1502 return static_call(__perf_guest_state)();
1504 static inline unsigned long perf_guest_get_ip(void)
1506 return static_call(__perf_guest_get_ip)();
1508 static inline unsigned int perf_guest_handle_intel_pt_intr(void)
1510 return static_call(__perf_guest_handle_intel_pt_intr)();
1512 extern void perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs);
1513 extern void perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs);
1515 static inline unsigned int perf_guest_state(void) { return 0; }
1516 static inline unsigned long perf_guest_get_ip(void) { return 0; }
1517 static inline unsigned int perf_guest_handle_intel_pt_intr(void) { return 0; }
1518 #endif /* CONFIG_GUEST_PERF_EVENTS */
1520 extern void perf_event_exec(void);
1521 extern void perf_event_comm(struct task_struct *tsk, bool exec);
1522 extern void perf_event_namespaces(struct task_struct *tsk);
1523 extern void perf_event_fork(struct task_struct *tsk);
1524 extern void perf_event_text_poke(const void *addr,
1525 const void *old_bytes, size_t old_len,
1526 const void *new_bytes, size_t new_len);
1529 DECLARE_PER_CPU(struct perf_callchain_entry, perf_callchain_entry);
1531 extern void perf_callchain_user(struct perf_callchain_entry_ctx *entry, struct pt_regs *regs);
1532 extern void perf_callchain_kernel(struct perf_callchain_entry_ctx *entry, struct pt_regs *regs);
1533 extern struct perf_callchain_entry *
1534 get_perf_callchain(struct pt_regs *regs, u32 init_nr, bool kernel, bool user,
1535 u32 max_stack, bool crosstask, bool add_mark);
1536 extern int get_callchain_buffers(int max_stack);
1537 extern void put_callchain_buffers(void);
1538 extern struct perf_callchain_entry *get_callchain_entry(int *rctx);
1539 extern void put_callchain_entry(int rctx);
1541 extern int sysctl_perf_event_max_stack;
1542 extern int sysctl_perf_event_max_contexts_per_stack;
1544 static inline int perf_callchain_store_context(struct perf_callchain_entry_ctx *ctx, u64 ip)
1546 if (ctx->contexts < sysctl_perf_event_max_contexts_per_stack) {
1547 struct perf_callchain_entry *entry = ctx->entry;
1548 entry->ip[entry->nr++] = ip;
1552 ctx->contexts_maxed = true;
1553 return -1; /* no more room, stop walking the stack */
1557 static inline int perf_callchain_store(struct perf_callchain_entry_ctx *ctx, u64 ip)
1559 if (ctx->nr < ctx->max_stack && !ctx->contexts_maxed) {
1560 struct perf_callchain_entry *entry = ctx->entry;
1561 entry->ip[entry->nr++] = ip;
1565 return -1; /* no more room, stop walking the stack */
1569 extern int sysctl_perf_event_paranoid;
1570 extern int sysctl_perf_event_mlock;
1571 extern int sysctl_perf_event_sample_rate;
1572 extern int sysctl_perf_cpu_time_max_percent;
1574 extern void perf_sample_event_took(u64 sample_len_ns);
1576 int perf_proc_update_handler(struct ctl_table *table, int write,
1577 void *buffer, size_t *lenp, loff_t *ppos);
1578 int perf_cpu_time_max_percent_handler(struct ctl_table *table, int write,
1579 void *buffer, size_t *lenp, loff_t *ppos);
1580 int perf_event_max_stack_handler(struct ctl_table *table, int write,
1581 void *buffer, size_t *lenp, loff_t *ppos);
1583 /* Access to perf_event_open(2) syscall. */
1584 #define PERF_SECURITY_OPEN 0
1586 /* Finer grained perf_event_open(2) access control. */
1587 #define PERF_SECURITY_CPU 1
1588 #define PERF_SECURITY_KERNEL 2
1589 #define PERF_SECURITY_TRACEPOINT 3
1591 static inline int perf_is_paranoid(void)
1593 return sysctl_perf_event_paranoid > -1;
1596 static inline int perf_allow_kernel(struct perf_event_attr *attr)
1598 if (sysctl_perf_event_paranoid > 1 && !perfmon_capable())
1601 return security_perf_event_open(attr, PERF_SECURITY_KERNEL);
1604 static inline int perf_allow_cpu(struct perf_event_attr *attr)
1606 if (sysctl_perf_event_paranoid > 0 && !perfmon_capable())
1609 return security_perf_event_open(attr, PERF_SECURITY_CPU);
1612 static inline int perf_allow_tracepoint(struct perf_event_attr *attr)
1614 if (sysctl_perf_event_paranoid > -1 && !perfmon_capable())
1617 return security_perf_event_open(attr, PERF_SECURITY_TRACEPOINT);
1620 extern void perf_event_init(void);
1621 extern void perf_tp_event(u16 event_type, u64 count, void *record,
1622 int entry_size, struct pt_regs *regs,
1623 struct hlist_head *head, int rctx,
1624 struct task_struct *task);
1625 extern void perf_bp_event(struct perf_event *event, void *data);
1627 #ifndef perf_misc_flags
1628 # define perf_misc_flags(regs) \
1629 (user_mode(regs) ? PERF_RECORD_MISC_USER : PERF_RECORD_MISC_KERNEL)
1630 # define perf_instruction_pointer(regs) instruction_pointer(regs)
1632 #ifndef perf_arch_bpf_user_pt_regs
1633 # define perf_arch_bpf_user_pt_regs(regs) regs
1636 static inline bool has_branch_stack(struct perf_event *event)
1638 return event->attr.sample_type & PERF_SAMPLE_BRANCH_STACK;
1641 static inline bool needs_branch_stack(struct perf_event *event)
1643 return event->attr.branch_sample_type != 0;
1646 static inline bool has_aux(struct perf_event *event)
1648 return event->pmu->setup_aux;
1651 static inline bool is_write_backward(struct perf_event *event)
1653 return !!event->attr.write_backward;
1656 static inline bool has_addr_filter(struct perf_event *event)
1658 return event->pmu->nr_addr_filters;
1662 * An inherited event uses parent's filters
1664 static inline struct perf_addr_filters_head *
1665 perf_event_addr_filters(struct perf_event *event)
1667 struct perf_addr_filters_head *ifh = &event->addr_filters;
1670 ifh = &event->parent->addr_filters;
1675 extern void perf_event_addr_filters_sync(struct perf_event *event);
1676 extern void perf_report_aux_output_id(struct perf_event *event, u64 hw_id);
1678 extern int perf_output_begin(struct perf_output_handle *handle,
1679 struct perf_sample_data *data,
1680 struct perf_event *event, unsigned int size);
1681 extern int perf_output_begin_forward(struct perf_output_handle *handle,
1682 struct perf_sample_data *data,
1683 struct perf_event *event,
1685 extern int perf_output_begin_backward(struct perf_output_handle *handle,
1686 struct perf_sample_data *data,
1687 struct perf_event *event,
1690 extern void perf_output_end(struct perf_output_handle *handle);
1691 extern unsigned int perf_output_copy(struct perf_output_handle *handle,
1692 const void *buf, unsigned int len);
1693 extern unsigned int perf_output_skip(struct perf_output_handle *handle,
1695 extern long perf_output_copy_aux(struct perf_output_handle *aux_handle,
1696 struct perf_output_handle *handle,
1697 unsigned long from, unsigned long to);
1698 extern int perf_swevent_get_recursion_context(void);
1699 extern void perf_swevent_put_recursion_context(int rctx);
1700 extern u64 perf_swevent_set_period(struct perf_event *event);
1701 extern void perf_event_enable(struct perf_event *event);
1702 extern void perf_event_disable(struct perf_event *event);
1703 extern void perf_event_disable_local(struct perf_event *event);
1704 extern void perf_event_disable_inatomic(struct perf_event *event);
1705 extern void perf_event_task_tick(void);
1706 extern int perf_event_account_interrupt(struct perf_event *event);
1707 extern int perf_event_period(struct perf_event *event, u64 value);
1708 extern u64 perf_event_pause(struct perf_event *event, bool reset);
1709 #else /* !CONFIG_PERF_EVENTS: */
1710 static inline void *
1711 perf_aux_output_begin(struct perf_output_handle *handle,
1712 struct perf_event *event) { return NULL; }
1714 perf_aux_output_end(struct perf_output_handle *handle, unsigned long size)
1717 perf_aux_output_skip(struct perf_output_handle *handle,
1718 unsigned long size) { return -EINVAL; }
1719 static inline void *
1720 perf_get_aux(struct perf_output_handle *handle) { return NULL; }
1722 perf_event_task_migrate(struct task_struct *task) { }
1724 perf_event_task_sched_in(struct task_struct *prev,
1725 struct task_struct *task) { }
1727 perf_event_task_sched_out(struct task_struct *prev,
1728 struct task_struct *next) { }
1729 static inline int perf_event_init_task(struct task_struct *child,
1730 u64 clone_flags) { return 0; }
1731 static inline void perf_event_exit_task(struct task_struct *child) { }
1732 static inline void perf_event_free_task(struct task_struct *task) { }
1733 static inline void perf_event_delayed_put(struct task_struct *task) { }
1734 static inline struct file *perf_event_get(unsigned int fd) { return ERR_PTR(-EINVAL); }
1735 static inline const struct perf_event *perf_get_event(struct file *file)
1737 return ERR_PTR(-EINVAL);
1739 static inline const struct perf_event_attr *perf_event_attrs(struct perf_event *event)
1741 return ERR_PTR(-EINVAL);
1743 static inline int perf_event_read_local(struct perf_event *event, u64 *value,
1744 u64 *enabled, u64 *running)
1748 static inline void perf_event_print_debug(void) { }
1749 static inline int perf_event_task_disable(void) { return -EINVAL; }
1750 static inline int perf_event_task_enable(void) { return -EINVAL; }
1751 static inline int perf_event_refresh(struct perf_event *event, int refresh)
1757 perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr) { }
1759 perf_bp_event(struct perf_event *event, void *data) { }
1761 static inline void perf_event_mmap(struct vm_area_struct *vma) { }
1763 typedef int (perf_ksymbol_get_name_f)(char *name, int name_len, void *data);
1764 static inline void perf_event_ksymbol(u16 ksym_type, u64 addr, u32 len,
1765 bool unregister, const char *sym) { }
1766 static inline void perf_event_bpf_event(struct bpf_prog *prog,
1767 enum perf_bpf_event_type type,
1769 static inline void perf_event_exec(void) { }
1770 static inline void perf_event_comm(struct task_struct *tsk, bool exec) { }
1771 static inline void perf_event_namespaces(struct task_struct *tsk) { }
1772 static inline void perf_event_fork(struct task_struct *tsk) { }
1773 static inline void perf_event_text_poke(const void *addr,
1774 const void *old_bytes,
1776 const void *new_bytes,
1778 static inline void perf_event_init(void) { }
1779 static inline int perf_swevent_get_recursion_context(void) { return -1; }
1780 static inline void perf_swevent_put_recursion_context(int rctx) { }
1781 static inline u64 perf_swevent_set_period(struct perf_event *event) { return 0; }
1782 static inline void perf_event_enable(struct perf_event *event) { }
1783 static inline void perf_event_disable(struct perf_event *event) { }
1784 static inline int __perf_event_disable(void *info) { return -1; }
1785 static inline void perf_event_task_tick(void) { }
1786 static inline int perf_event_release_kernel(struct perf_event *event) { return 0; }
1787 static inline int perf_event_period(struct perf_event *event, u64 value)
1791 static inline u64 perf_event_pause(struct perf_event *event, bool reset)
1797 #if defined(CONFIG_PERF_EVENTS) && defined(CONFIG_CPU_SUP_INTEL)
1798 extern void perf_restore_debug_store(void);
1800 static inline void perf_restore_debug_store(void) { }
1803 #define perf_output_put(handle, x) perf_output_copy((handle), &(x), sizeof(x))
1805 struct perf_pmu_events_attr {
1806 struct device_attribute attr;
1808 const char *event_str;
1811 struct perf_pmu_events_ht_attr {
1812 struct device_attribute attr;
1814 const char *event_str_ht;
1815 const char *event_str_noht;
1818 struct perf_pmu_events_hybrid_attr {
1819 struct device_attribute attr;
1821 const char *event_str;
1825 struct perf_pmu_format_hybrid_attr {
1826 struct device_attribute attr;
1830 ssize_t perf_event_sysfs_show(struct device *dev, struct device_attribute *attr,
1833 #define PMU_EVENT_ATTR(_name, _var, _id, _show) \
1834 static struct perf_pmu_events_attr _var = { \
1835 .attr = __ATTR(_name, 0444, _show, NULL), \
1839 #define PMU_EVENT_ATTR_STRING(_name, _var, _str) \
1840 static struct perf_pmu_events_attr _var = { \
1841 .attr = __ATTR(_name, 0444, perf_event_sysfs_show, NULL), \
1843 .event_str = _str, \
1846 #define PMU_EVENT_ATTR_ID(_name, _show, _id) \
1847 (&((struct perf_pmu_events_attr[]) { \
1848 { .attr = __ATTR(_name, 0444, _show, NULL), \
1852 #define PMU_FORMAT_ATTR_SHOW(_name, _format) \
1854 _name##_show(struct device *dev, \
1855 struct device_attribute *attr, \
1858 BUILD_BUG_ON(sizeof(_format) >= PAGE_SIZE); \
1859 return sprintf(page, _format "\n"); \
1862 #define PMU_FORMAT_ATTR(_name, _format) \
1863 PMU_FORMAT_ATTR_SHOW(_name, _format) \
1865 static struct device_attribute format_attr_##_name = __ATTR_RO(_name)
1867 /* Performance counter hotplug functions */
1868 #ifdef CONFIG_PERF_EVENTS
1869 int perf_event_init_cpu(unsigned int cpu);
1870 int perf_event_exit_cpu(unsigned int cpu);
1872 #define perf_event_init_cpu NULL
1873 #define perf_event_exit_cpu NULL
1876 extern void arch_perf_update_userpage(struct perf_event *event,
1877 struct perf_event_mmap_page *userpg,
1881 * Snapshot branch stack on software events.
1883 * Branch stack can be very useful in understanding software events. For
1884 * example, when a long function, e.g. sys_perf_event_open, returns an
1885 * errno, it is not obvious why the function failed. Branch stack could
1886 * provide very helpful information in this type of scenarios.
1888 * On software event, it is necessary to stop the hardware branch recorder
1889 * fast. Otherwise, the hardware register/buffer will be flushed with
1890 * entries of the triggering event. Therefore, static call is used to
1891 * stop the hardware recorder.
1895 * cnt is the number of entries allocated for entries.
1896 * Return number of entries copied to .
1898 typedef int (perf_snapshot_branch_stack_t)(struct perf_branch_entry *entries,
1900 DECLARE_STATIC_CALL(perf_snapshot_branch_stack, perf_snapshot_branch_stack_t);
1902 #ifndef PERF_NEEDS_LOPWR_CB
1903 static inline void perf_lopwr_cb(bool mode)
1908 #endif /* _LINUX_PERF_EVENT_H */