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_HETEROGENEOUS_CPUS 0x0040
292 #define PERF_PMU_CAP_NO_EXCLUDE 0x0080
293 #define PERF_PMU_CAP_AUX_OUTPUT 0x0100
294 #define PERF_PMU_CAP_EXTENDED_HW_TYPE 0x0200
296 struct perf_output_handle;
299 * struct pmu - generic performance monitoring unit
302 struct list_head entry;
304 struct module *module;
306 const struct attribute_group **attr_groups;
307 const struct attribute_group **attr_update;
312 * various common per-pmu feature flags
316 int __percpu *pmu_disable_count;
317 struct perf_cpu_pmu_context __percpu *cpu_pmu_context;
318 atomic_t exclusive_cnt; /* < 0: cpu; > 0: tsk */
320 int hrtimer_interval_ms;
322 /* number of address filters this PMU can do */
323 unsigned int nr_addr_filters;
326 * Fully disable/enable this PMU, can be used to protect from the PMI
327 * as well as for lazy/batch writing of the MSRs.
329 void (*pmu_enable) (struct pmu *pmu); /* optional */
330 void (*pmu_disable) (struct pmu *pmu); /* optional */
333 * Try and initialize the event for this PMU.
336 * -ENOENT -- @event is not for this PMU
338 * -ENODEV -- @event is for this PMU but PMU not present
339 * -EBUSY -- @event is for this PMU but PMU temporarily unavailable
340 * -EINVAL -- @event is for this PMU but @event is not valid
341 * -EOPNOTSUPP -- @event is for this PMU, @event is valid, but not supported
342 * -EACCES -- @event is for this PMU, @event is valid, but no privileges
344 * 0 -- @event is for this PMU and valid
346 * Other error return values are allowed.
348 int (*event_init) (struct perf_event *event);
351 * Notification that the event was mapped or unmapped. Called
352 * in the context of the mapping task.
354 void (*event_mapped) (struct perf_event *event, struct mm_struct *mm); /* optional */
355 void (*event_unmapped) (struct perf_event *event, struct mm_struct *mm); /* optional */
358 * Flags for ->add()/->del()/ ->start()/->stop(). There are
359 * matching hw_perf_event::state flags.
361 #define PERF_EF_START 0x01 /* start the counter when adding */
362 #define PERF_EF_RELOAD 0x02 /* reload the counter when starting */
363 #define PERF_EF_UPDATE 0x04 /* update the counter when stopping */
366 * Adds/Removes a counter to/from the PMU, can be done inside a
367 * transaction, see the ->*_txn() methods.
369 * The add/del callbacks will reserve all hardware resources required
370 * to service the event, this includes any counter constraint
373 * Called with IRQs disabled and the PMU disabled on the CPU the event
376 * ->add() called without PERF_EF_START should result in the same state
377 * as ->add() followed by ->stop().
379 * ->del() must always PERF_EF_UPDATE stop an event. If it calls
380 * ->stop() that must deal with already being stopped without
383 int (*add) (struct perf_event *event, int flags);
384 void (*del) (struct perf_event *event, int flags);
387 * Starts/Stops a counter present on the PMU.
389 * The PMI handler should stop the counter when perf_event_overflow()
390 * returns !0. ->start() will be used to continue.
392 * Also used to change the sample period.
394 * Called with IRQs disabled and the PMU disabled on the CPU the event
395 * is on -- will be called from NMI context with the PMU generates
398 * ->stop() with PERF_EF_UPDATE will read the counter and update
399 * period/count values like ->read() would.
401 * ->start() with PERF_EF_RELOAD will reprogram the counter
402 * value, must be preceded by a ->stop() with PERF_EF_UPDATE.
404 void (*start) (struct perf_event *event, int flags);
405 void (*stop) (struct perf_event *event, int flags);
408 * Updates the counter value of the event.
410 * For sampling capable PMUs this will also update the software period
411 * hw_perf_event::period_left field.
413 void (*read) (struct perf_event *event);
416 * Group events scheduling is treated as a transaction, add
417 * group events as a whole and perform one schedulability test.
418 * If the test fails, roll back the whole group
420 * Start the transaction, after this ->add() doesn't need to
421 * do schedulability tests.
425 void (*start_txn) (struct pmu *pmu, unsigned int txn_flags);
427 * If ->start_txn() disabled the ->add() schedulability test
428 * then ->commit_txn() is required to perform one. On success
429 * the transaction is closed. On error the transaction is kept
430 * open until ->cancel_txn() is called.
434 int (*commit_txn) (struct pmu *pmu);
436 * Will cancel the transaction, assumes ->del() is called
437 * for each successful ->add() during the transaction.
441 void (*cancel_txn) (struct pmu *pmu);
444 * Will return the value for perf_event_mmap_page::index for this event,
445 * if no implementation is provided it will default to: event->hw.idx + 1.
447 int (*event_idx) (struct perf_event *event); /*optional */
450 * context-switches callback
452 void (*sched_task) (struct perf_event_pmu_context *pmu_ctx,
456 * Kmem cache of PMU specific data
458 struct kmem_cache *task_ctx_cache;
461 * PMU specific parts of task perf event context (i.e. ctx->task_ctx_data)
462 * can be synchronized using this function. See Intel LBR callstack support
463 * implementation and Perf core context switch handling callbacks for usage
466 void (*swap_task_ctx) (struct perf_event_pmu_context *prev_epc,
467 struct perf_event_pmu_context *next_epc);
471 * Set up pmu-private data structures for an AUX area
473 void *(*setup_aux) (struct perf_event *event, void **pages,
474 int nr_pages, bool overwrite);
478 * Free pmu-private AUX data structures
480 void (*free_aux) (void *aux); /* optional */
483 * Take a snapshot of the AUX buffer without touching the event
484 * state, so that preempting ->start()/->stop() callbacks does
485 * not interfere with their logic. Called in PMI context.
487 * Returns the size of AUX data copied to the output handle.
491 long (*snapshot_aux) (struct perf_event *event,
492 struct perf_output_handle *handle,
496 * Validate address range filters: make sure the HW supports the
497 * requested configuration and number of filters; return 0 if the
498 * supplied filters are valid, -errno otherwise.
500 * Runs in the context of the ioctl()ing process and is not serialized
501 * with the rest of the PMU callbacks.
503 int (*addr_filters_validate) (struct list_head *filters);
507 * Synchronize address range filter configuration:
508 * translate hw-agnostic filters into hardware configuration in
509 * event::hw::addr_filters.
511 * Runs as a part of filter sync sequence that is done in ->start()
512 * callback by calling perf_event_addr_filters_sync().
514 * May (and should) traverse event::addr_filters::list, for which its
515 * caller provides necessary serialization.
517 void (*addr_filters_sync) (struct perf_event *event);
521 * Check if event can be used for aux_output purposes for
522 * events of this PMU.
524 * Runs from perf_event_open(). Should return 0 for "no match"
525 * or non-zero for "match".
527 int (*aux_output_match) (struct perf_event *event);
531 * Skip programming this PMU on the given CPU. Typically needed for
534 bool (*filter) (struct pmu *pmu, int cpu); /* optional */
537 * Check period value for PERF_EVENT_IOC_PERIOD ioctl.
539 int (*check_period) (struct perf_event *event, u64 value); /* optional */
542 enum perf_addr_filter_action_t {
543 PERF_ADDR_FILTER_ACTION_STOP = 0,
544 PERF_ADDR_FILTER_ACTION_START,
545 PERF_ADDR_FILTER_ACTION_FILTER,
549 * struct perf_addr_filter - address range filter definition
550 * @entry: event's filter list linkage
551 * @path: object file's path for file-based filters
552 * @offset: filter range offset
553 * @size: filter range size (size==0 means single address trigger)
554 * @action: filter/start/stop
556 * This is a hardware-agnostic filter configuration as specified by the user.
558 struct perf_addr_filter {
559 struct list_head entry;
561 unsigned long offset;
563 enum perf_addr_filter_action_t action;
567 * struct perf_addr_filters_head - container for address range filters
568 * @list: list of filters for this event
569 * @lock: spinlock that serializes accesses to the @list and event's
570 * (and its children's) filter generations.
571 * @nr_file_filters: number of file-based filters
573 * A child event will use parent's @list (and therefore @lock), so they are
574 * bundled together; see perf_event_addr_filters().
576 struct perf_addr_filters_head {
577 struct list_head list;
579 unsigned int nr_file_filters;
582 struct perf_addr_filter_range {
588 * enum perf_event_state - the states of an event:
590 enum perf_event_state {
591 PERF_EVENT_STATE_DEAD = -4,
592 PERF_EVENT_STATE_EXIT = -3,
593 PERF_EVENT_STATE_ERROR = -2,
594 PERF_EVENT_STATE_OFF = -1,
595 PERF_EVENT_STATE_INACTIVE = 0,
596 PERF_EVENT_STATE_ACTIVE = 1,
600 struct perf_sample_data;
602 typedef void (*perf_overflow_handler_t)(struct perf_event *,
603 struct perf_sample_data *,
604 struct pt_regs *regs);
607 * Event capabilities. For event_caps and groups caps.
609 * PERF_EV_CAP_SOFTWARE: Is a software event.
610 * PERF_EV_CAP_READ_ACTIVE_PKG: A CPU event (or cgroup event) that can be read
611 * from any CPU in the package where it is active.
612 * PERF_EV_CAP_SIBLING: An event with this flag must be a group sibling and
613 * cannot be a group leader. If an event with this flag is detached from the
614 * group it is scheduled out and moved into an unrecoverable ERROR state.
616 #define PERF_EV_CAP_SOFTWARE BIT(0)
617 #define PERF_EV_CAP_READ_ACTIVE_PKG BIT(1)
618 #define PERF_EV_CAP_SIBLING BIT(2)
620 #define SWEVENT_HLIST_BITS 8
621 #define SWEVENT_HLIST_SIZE (1 << SWEVENT_HLIST_BITS)
623 struct swevent_hlist {
624 struct hlist_head heads[SWEVENT_HLIST_SIZE];
625 struct rcu_head rcu_head;
628 #define PERF_ATTACH_CONTEXT 0x01
629 #define PERF_ATTACH_GROUP 0x02
630 #define PERF_ATTACH_TASK 0x04
631 #define PERF_ATTACH_TASK_DATA 0x08
632 #define PERF_ATTACH_ITRACE 0x10
633 #define PERF_ATTACH_SCHED_CB 0x20
634 #define PERF_ATTACH_CHILD 0x40
640 struct pmu_event_list {
642 struct list_head list;
646 * event->sibling_list is modified whole holding both ctx->lock and ctx->mutex
647 * as such iteration must hold either lock. However, since ctx->lock is an IRQ
648 * safe lock, and is only held by the CPU doing the modification, having IRQs
649 * disabled is sufficient since it will hold-off the IPIs.
651 #ifdef CONFIG_PROVE_LOCKING
652 #define lockdep_assert_event_ctx(event) \
653 WARN_ON_ONCE(__lockdep_enabled && \
654 (this_cpu_read(hardirqs_enabled) && \
655 lockdep_is_held(&(event)->ctx->mutex) != LOCK_STATE_HELD))
657 #define lockdep_assert_event_ctx(event)
660 #define for_each_sibling_event(sibling, event) \
661 lockdep_assert_event_ctx(event); \
662 if ((event)->group_leader == (event)) \
663 list_for_each_entry((sibling), &(event)->sibling_list, sibling_list)
666 * struct perf_event - performance event kernel representation:
669 #ifdef CONFIG_PERF_EVENTS
671 * entry onto perf_event_context::event_list;
672 * modifications require ctx->lock
673 * RCU safe iterations.
675 struct list_head event_entry;
678 * Locked for modification by both ctx->mutex and ctx->lock; holding
679 * either sufficies for read.
681 struct list_head sibling_list;
682 struct list_head active_list;
684 * Node on the pinned or flexible tree located at the event context;
686 struct rb_node group_node;
689 * We need storage to track the entries in perf_pmu_migrate_context; we
690 * cannot use the event_entry because of RCU and we want to keep the
691 * group in tact which avoids us using the other two entries.
693 struct list_head migrate_entry;
695 struct hlist_node hlist_entry;
696 struct list_head active_entry;
699 /* Not serialized. Only written during event initialization. */
701 /* The cumulative AND of all event_caps for events in this group. */
704 struct perf_event *group_leader;
706 * event->pmu will always point to pmu in which this event belongs.
707 * Whereas event->pmu_ctx->pmu may point to other pmu when group of
708 * different pmu events is created.
713 enum perf_event_state state;
714 unsigned int attach_state;
716 atomic64_t child_count;
719 * These are the total time in nanoseconds that the event
720 * has been enabled (i.e. eligible to run, and the task has
721 * been scheduled in, if this is a per-task event)
722 * and running (scheduled onto the CPU), respectively.
724 u64 total_time_enabled;
725 u64 total_time_running;
728 struct perf_event_attr attr;
732 struct hw_perf_event hw;
734 struct perf_event_context *ctx;
736 * event->pmu_ctx points to perf_event_pmu_context in which the event
737 * is added. This pmu_ctx can be of other pmu for sw event when that
738 * sw event is part of a group which also contains non-sw events.
740 struct perf_event_pmu_context *pmu_ctx;
741 atomic_long_t refcount;
744 * These accumulate total time (in nanoseconds) that children
745 * events have been enabled and running, respectively.
747 atomic64_t child_total_time_enabled;
748 atomic64_t child_total_time_running;
751 * Protect attach/detach and child_list:
753 struct mutex child_mutex;
754 struct list_head child_list;
755 struct perf_event *parent;
760 struct list_head owner_entry;
761 struct task_struct *owner;
764 struct mutex mmap_mutex;
767 struct perf_buffer *rb;
768 struct list_head rb_entry;
769 unsigned long rcu_batches;
773 wait_queue_head_t waitq;
774 struct fasync_struct *fasync;
776 /* delayed work for NMIs and such */
777 unsigned int pending_wakeup;
778 unsigned int pending_kill;
779 unsigned int pending_disable;
780 unsigned int pending_sigtrap;
781 unsigned long pending_addr; /* SIGTRAP */
782 struct irq_work pending_irq;
783 struct callback_head pending_task;
784 unsigned int pending_work;
786 atomic_t event_limit;
788 /* address range filters */
789 struct perf_addr_filters_head addr_filters;
790 /* vma address array for file-based filders */
791 struct perf_addr_filter_range *addr_filter_ranges;
792 unsigned long addr_filters_gen;
794 /* for aux_output events */
795 struct perf_event *aux_event;
797 void (*destroy)(struct perf_event *);
798 struct rcu_head rcu_head;
800 struct pid_namespace *ns;
803 atomic64_t lost_samples;
806 perf_overflow_handler_t overflow_handler;
807 void *overflow_handler_context;
808 #ifdef CONFIG_BPF_SYSCALL
809 perf_overflow_handler_t orig_overflow_handler;
810 struct bpf_prog *prog;
814 #ifdef CONFIG_EVENT_TRACING
815 struct trace_event_call *tp_event;
816 struct event_filter *filter;
817 #ifdef CONFIG_FUNCTION_TRACER
818 struct ftrace_ops ftrace_ops;
822 #ifdef CONFIG_CGROUP_PERF
823 struct perf_cgroup *cgrp; /* cgroup event is attach to */
826 #ifdef CONFIG_SECURITY
829 struct list_head sb_list;
830 #endif /* CONFIG_PERF_EVENTS */
834 * ,-----------------------[1:n]----------------------.
836 * perf_event_context <-[1:n]-> perf_event_pmu_context <--- perf_event
838 * `--------[1:n]---------' `-[n:1]-> pmu <-[1:n]-'
841 * struct perf_event_pmu_context lifetime is refcount based and RCU freed
842 * (similar to perf_event_context). Locking is as if it were a member of
843 * perf_event_context; specifically:
845 * modification, both: ctx->mutex && ctx->lock
846 * reading, either: ctx->mutex || ctx->lock
848 * There is one exception to this; namely put_pmu_ctx() isn't always called
849 * with ctx->mutex held; this means that as long as we can guarantee the epc
850 * has events the above rules hold.
852 * Specificially, sys_perf_event_open()'s group_leader case depends on
853 * ctx->mutex pinning the configuration. Since we hold a reference on
854 * group_leader (through the filedesc) it can't go away, therefore it's
855 * associated pmu_ctx must exist and cannot change due to ctx->mutex.
857 struct perf_event_pmu_context {
859 struct perf_event_context *ctx;
861 struct list_head pmu_ctx_entry;
863 struct list_head pinned_active;
864 struct list_head flexible_active;
866 /* Used to avoid freeing per-cpu perf_event_pmu_context */
867 unsigned int embedded : 1;
869 unsigned int nr_events;
871 atomic_t refcount; /* event <-> epc */
872 struct rcu_head rcu_head;
874 void *task_ctx_data; /* pmu specific data */
876 * Set when one or more (plausibly active) event can't be scheduled
877 * due to pmu overcommit or pmu constraints, except tolerant to
878 * events not necessary to be active due to scheduling constraints,
881 int rotate_necessary;
884 struct perf_event_groups {
891 * struct perf_event_context - event context structure
893 * Used as a container for task events and CPU events as well:
895 struct perf_event_context {
897 * Protect the states of the events in the list,
898 * nr_active, and the list:
902 * Protect the list of events. Locking either mutex or lock
903 * is sufficient to ensure the list doesn't change; to change
904 * the list you need to lock both the mutex and the spinlock.
908 struct list_head pmu_ctx_list;
909 struct perf_event_groups pinned_groups;
910 struct perf_event_groups flexible_groups;
911 struct list_head event_list;
922 refcount_t refcount; /* event <-> ctx */
923 struct task_struct *task;
926 * Context clock, runs when context enabled.
933 * These fields let us detect when two contexts have both
934 * been cloned (inherited) from a common ancestor.
936 struct perf_event_context *parent_ctx;
940 #ifdef CONFIG_CGROUP_PERF
941 int nr_cgroups; /* cgroup evts */
943 struct rcu_head rcu_head;
946 * Sum (event->pending_sigtrap + event->pending_work)
948 * The SIGTRAP is targeted at ctx->task, as such it won't do changing
949 * that until the signal is delivered.
955 * Number of contexts where an event can trigger:
956 * task, softirq, hardirq, nmi.
958 #define PERF_NR_CONTEXTS 4
960 struct perf_cpu_pmu_context {
961 struct perf_event_pmu_context epc;
962 struct perf_event_pmu_context *task_epc;
964 struct list_head sched_cb_entry;
970 raw_spinlock_t hrtimer_lock;
971 struct hrtimer hrtimer;
972 ktime_t hrtimer_interval;
973 unsigned int hrtimer_active;
977 * struct perf_event_cpu_context - per cpu event context structure
979 struct perf_cpu_context {
980 struct perf_event_context ctx;
981 struct perf_event_context *task_ctx;
984 #ifdef CONFIG_CGROUP_PERF
985 struct perf_cgroup *cgrp;
989 * Per-CPU storage for iterators used in visit_groups_merge. The default
990 * storage is of size 2 to hold the CPU and any CPU event iterators.
993 struct perf_event **heap;
994 struct perf_event *heap_default[2];
997 struct perf_output_handle {
998 struct perf_event *event;
999 struct perf_buffer *rb;
1000 unsigned long wakeup;
1010 struct bpf_perf_event_data_kern {
1011 bpf_user_pt_regs_t *regs;
1012 struct perf_sample_data *data;
1013 struct perf_event *event;
1016 #ifdef CONFIG_CGROUP_PERF
1019 * perf_cgroup_info keeps track of time_enabled for a cgroup.
1020 * This is a per-cpu dynamically allocated data structure.
1022 struct perf_cgroup_info {
1029 struct perf_cgroup {
1030 struct cgroup_subsys_state css;
1031 struct perf_cgroup_info __percpu *info;
1035 * Must ensure cgroup is pinned (css_get) before calling
1036 * this function. In other words, we cannot call this function
1037 * if there is no cgroup event for the current CPU context.
1039 static inline struct perf_cgroup *
1040 perf_cgroup_from_task(struct task_struct *task, struct perf_event_context *ctx)
1042 return container_of(task_css_check(task, perf_event_cgrp_id,
1043 ctx ? lockdep_is_held(&ctx->lock)
1045 struct perf_cgroup, css);
1047 #endif /* CONFIG_CGROUP_PERF */
1049 #ifdef CONFIG_PERF_EVENTS
1051 extern struct perf_event_context *perf_cpu_task_ctx(void);
1053 extern void *perf_aux_output_begin(struct perf_output_handle *handle,
1054 struct perf_event *event);
1055 extern void perf_aux_output_end(struct perf_output_handle *handle,
1056 unsigned long size);
1057 extern int perf_aux_output_skip(struct perf_output_handle *handle,
1058 unsigned long size);
1059 extern void *perf_get_aux(struct perf_output_handle *handle);
1060 extern void perf_aux_output_flag(struct perf_output_handle *handle, u64 flags);
1061 extern void perf_event_itrace_started(struct perf_event *event);
1063 extern int perf_pmu_register(struct pmu *pmu, const char *name, int type);
1064 extern void perf_pmu_unregister(struct pmu *pmu);
1066 extern void __perf_event_task_sched_in(struct task_struct *prev,
1067 struct task_struct *task);
1068 extern void __perf_event_task_sched_out(struct task_struct *prev,
1069 struct task_struct *next);
1070 extern int perf_event_init_task(struct task_struct *child, u64 clone_flags);
1071 extern void perf_event_exit_task(struct task_struct *child);
1072 extern void perf_event_free_task(struct task_struct *task);
1073 extern void perf_event_delayed_put(struct task_struct *task);
1074 extern struct file *perf_event_get(unsigned int fd);
1075 extern const struct perf_event *perf_get_event(struct file *file);
1076 extern const struct perf_event_attr *perf_event_attrs(struct perf_event *event);
1077 extern void perf_event_print_debug(void);
1078 extern void perf_pmu_disable(struct pmu *pmu);
1079 extern void perf_pmu_enable(struct pmu *pmu);
1080 extern void perf_sched_cb_dec(struct pmu *pmu);
1081 extern void perf_sched_cb_inc(struct pmu *pmu);
1082 extern int perf_event_task_disable(void);
1083 extern int perf_event_task_enable(void);
1085 extern void perf_pmu_resched(struct pmu *pmu);
1087 extern int perf_event_refresh(struct perf_event *event, int refresh);
1088 extern void perf_event_update_userpage(struct perf_event *event);
1089 extern int perf_event_release_kernel(struct perf_event *event);
1090 extern struct perf_event *
1091 perf_event_create_kernel_counter(struct perf_event_attr *attr,
1093 struct task_struct *task,
1094 perf_overflow_handler_t callback,
1096 extern void perf_pmu_migrate_context(struct pmu *pmu,
1097 int src_cpu, int dst_cpu);
1098 int perf_event_read_local(struct perf_event *event, u64 *value,
1099 u64 *enabled, u64 *running);
1100 extern u64 perf_event_read_value(struct perf_event *event,
1101 u64 *enabled, u64 *running);
1103 extern struct perf_callchain_entry *perf_callchain(struct perf_event *event, struct pt_regs *regs);
1105 static inline bool branch_sample_no_flags(const struct perf_event *event)
1107 return event->attr.branch_sample_type & PERF_SAMPLE_BRANCH_NO_FLAGS;
1110 static inline bool branch_sample_no_cycles(const struct perf_event *event)
1112 return event->attr.branch_sample_type & PERF_SAMPLE_BRANCH_NO_CYCLES;
1115 static inline bool branch_sample_type(const struct perf_event *event)
1117 return event->attr.branch_sample_type & PERF_SAMPLE_BRANCH_TYPE_SAVE;
1120 static inline bool branch_sample_hw_index(const struct perf_event *event)
1122 return event->attr.branch_sample_type & PERF_SAMPLE_BRANCH_HW_INDEX;
1125 static inline bool branch_sample_priv(const struct perf_event *event)
1127 return event->attr.branch_sample_type & PERF_SAMPLE_BRANCH_PRIV_SAVE;
1131 struct perf_sample_data {
1133 * Fields set by perf_sample_data_init() unconditionally,
1134 * group so as to minimize the cachelines touched.
1141 * Fields commonly set by __perf_event_header__init_id(),
1142 * group so as to minimize the cachelines touched.
1157 * The other fields, optionally {set,used} by
1158 * perf_{prepare,output}_sample().
1161 struct perf_callchain_entry *callchain;
1162 struct perf_raw_record *raw;
1163 struct perf_branch_stack *br_stack;
1164 union perf_sample_weight weight;
1165 union perf_mem_data_src data_src;
1168 struct perf_regs regs_user;
1169 struct perf_regs regs_intr;
1170 u64 stack_user_size;
1179 } ____cacheline_aligned;
1181 /* default value for data source */
1182 #define PERF_MEM_NA (PERF_MEM_S(OP, NA) |\
1183 PERF_MEM_S(LVL, NA) |\
1184 PERF_MEM_S(SNOOP, NA) |\
1185 PERF_MEM_S(LOCK, NA) |\
1186 PERF_MEM_S(TLB, NA))
1188 static inline void perf_sample_data_init(struct perf_sample_data *data,
1189 u64 addr, u64 period)
1191 /* remaining struct members initialized in perf_prepare_sample() */
1192 data->sample_flags = PERF_SAMPLE_PERIOD;
1193 data->period = period;
1198 data->sample_flags |= PERF_SAMPLE_ADDR;
1202 static inline void perf_sample_save_callchain(struct perf_sample_data *data,
1203 struct perf_event *event,
1204 struct pt_regs *regs)
1208 data->callchain = perf_callchain(event, regs);
1209 size += data->callchain->nr;
1211 data->dyn_size += size * sizeof(u64);
1212 data->sample_flags |= PERF_SAMPLE_CALLCHAIN;
1215 static inline void perf_sample_save_raw_data(struct perf_sample_data *data,
1216 struct perf_raw_record *raw)
1218 struct perf_raw_frag *frag = &raw->frag;
1224 if (perf_raw_frag_last(frag))
1229 size = round_up(sum + sizeof(u32), sizeof(u64));
1230 raw->size = size - sizeof(u32);
1231 frag->pad = raw->size - sum;
1234 data->dyn_size += size;
1235 data->sample_flags |= PERF_SAMPLE_RAW;
1238 static inline void perf_sample_save_brstack(struct perf_sample_data *data,
1239 struct perf_event *event,
1240 struct perf_branch_stack *brs)
1242 int size = sizeof(u64); /* nr */
1244 if (branch_sample_hw_index(event))
1245 size += sizeof(u64);
1246 size += brs->nr * sizeof(struct perf_branch_entry);
1248 data->br_stack = brs;
1249 data->dyn_size += size;
1250 data->sample_flags |= PERF_SAMPLE_BRANCH_STACK;
1253 static inline u32 perf_sample_data_size(struct perf_sample_data *data,
1254 struct perf_event *event)
1256 u32 size = sizeof(struct perf_event_header);
1258 size += event->header_size + event->id_header_size;
1259 size += data->dyn_size;
1265 * Clear all bitfields in the perf_branch_entry.
1266 * The to and from fields are not cleared because they are
1267 * systematically modified by caller.
1269 static inline void perf_clear_branch_entry_bitfields(struct perf_branch_entry *br)
1277 br->spec = PERF_BR_SPEC_NA;
1281 extern void perf_output_sample(struct perf_output_handle *handle,
1282 struct perf_event_header *header,
1283 struct perf_sample_data *data,
1284 struct perf_event *event);
1285 extern void perf_prepare_sample(struct perf_sample_data *data,
1286 struct perf_event *event,
1287 struct pt_regs *regs);
1288 extern void perf_prepare_header(struct perf_event_header *header,
1289 struct perf_sample_data *data,
1290 struct perf_event *event,
1291 struct pt_regs *regs);
1293 extern int perf_event_overflow(struct perf_event *event,
1294 struct perf_sample_data *data,
1295 struct pt_regs *regs);
1297 extern void perf_event_output_forward(struct perf_event *event,
1298 struct perf_sample_data *data,
1299 struct pt_regs *regs);
1300 extern void perf_event_output_backward(struct perf_event *event,
1301 struct perf_sample_data *data,
1302 struct pt_regs *regs);
1303 extern int perf_event_output(struct perf_event *event,
1304 struct perf_sample_data *data,
1305 struct pt_regs *regs);
1308 is_default_overflow_handler(struct perf_event *event)
1310 if (likely(event->overflow_handler == perf_event_output_forward))
1312 if (unlikely(event->overflow_handler == perf_event_output_backward))
1318 perf_event_header__init_id(struct perf_event_header *header,
1319 struct perf_sample_data *data,
1320 struct perf_event *event);
1322 perf_event__output_id_sample(struct perf_event *event,
1323 struct perf_output_handle *handle,
1324 struct perf_sample_data *sample);
1327 perf_log_lost_samples(struct perf_event *event, u64 lost);
1329 static inline bool event_has_any_exclude_flag(struct perf_event *event)
1331 struct perf_event_attr *attr = &event->attr;
1333 return attr->exclude_idle || attr->exclude_user ||
1334 attr->exclude_kernel || attr->exclude_hv ||
1335 attr->exclude_guest || attr->exclude_host;
1338 static inline bool is_sampling_event(struct perf_event *event)
1340 return event->attr.sample_period != 0;
1344 * Return 1 for a software event, 0 for a hardware event
1346 static inline int is_software_event(struct perf_event *event)
1348 return event->event_caps & PERF_EV_CAP_SOFTWARE;
1352 * Return 1 for event in sw context, 0 for event in hw context
1354 static inline int in_software_context(struct perf_event *event)
1356 return event->pmu_ctx->pmu->task_ctx_nr == perf_sw_context;
1359 static inline int is_exclusive_pmu(struct pmu *pmu)
1361 return pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE;
1364 extern struct static_key perf_swevent_enabled[PERF_COUNT_SW_MAX];
1366 extern void ___perf_sw_event(u32, u64, struct pt_regs *, u64);
1367 extern void __perf_sw_event(u32, u64, struct pt_regs *, u64);
1369 #ifndef perf_arch_fetch_caller_regs
1370 static inline void perf_arch_fetch_caller_regs(struct pt_regs *regs, unsigned long ip) { }
1374 * When generating a perf sample in-line, instead of from an interrupt /
1375 * exception, we lack a pt_regs. This is typically used from software events
1376 * like: SW_CONTEXT_SWITCHES, SW_MIGRATIONS and the tie-in with tracepoints.
1378 * We typically don't need a full set, but (for x86) do require:
1379 * - ip for PERF_SAMPLE_IP
1380 * - cs for user_mode() tests
1381 * - sp for PERF_SAMPLE_CALLCHAIN
1382 * - eflags for MISC bits and CALLCHAIN (see: perf_hw_regs())
1384 * NOTE: assumes @regs is otherwise already 0 filled; this is important for
1385 * things like PERF_SAMPLE_REGS_INTR.
1387 static inline void perf_fetch_caller_regs(struct pt_regs *regs)
1389 perf_arch_fetch_caller_regs(regs, CALLER_ADDR0);
1392 static __always_inline void
1393 perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr)
1395 if (static_key_false(&perf_swevent_enabled[event_id]))
1396 __perf_sw_event(event_id, nr, regs, addr);
1399 DECLARE_PER_CPU(struct pt_regs, __perf_regs[4]);
1402 * 'Special' version for the scheduler, it hard assumes no recursion,
1403 * which is guaranteed by us not actually scheduling inside other swevents
1404 * because those disable preemption.
1406 static __always_inline void __perf_sw_event_sched(u32 event_id, u64 nr, u64 addr)
1408 struct pt_regs *regs = this_cpu_ptr(&__perf_regs[0]);
1410 perf_fetch_caller_regs(regs);
1411 ___perf_sw_event(event_id, nr, regs, addr);
1414 extern struct static_key_false perf_sched_events;
1416 static __always_inline bool __perf_sw_enabled(int swevt)
1418 return static_key_false(&perf_swevent_enabled[swevt]);
1421 static inline void perf_event_task_migrate(struct task_struct *task)
1423 if (__perf_sw_enabled(PERF_COUNT_SW_CPU_MIGRATIONS))
1424 task->sched_migrated = 1;
1427 static inline void perf_event_task_sched_in(struct task_struct *prev,
1428 struct task_struct *task)
1430 if (static_branch_unlikely(&perf_sched_events))
1431 __perf_event_task_sched_in(prev, task);
1433 if (__perf_sw_enabled(PERF_COUNT_SW_CPU_MIGRATIONS) &&
1434 task->sched_migrated) {
1435 __perf_sw_event_sched(PERF_COUNT_SW_CPU_MIGRATIONS, 1, 0);
1436 task->sched_migrated = 0;
1440 static inline void perf_event_task_sched_out(struct task_struct *prev,
1441 struct task_struct *next)
1443 if (__perf_sw_enabled(PERF_COUNT_SW_CONTEXT_SWITCHES))
1444 __perf_sw_event_sched(PERF_COUNT_SW_CONTEXT_SWITCHES, 1, 0);
1446 #ifdef CONFIG_CGROUP_PERF
1447 if (__perf_sw_enabled(PERF_COUNT_SW_CGROUP_SWITCHES) &&
1448 perf_cgroup_from_task(prev, NULL) !=
1449 perf_cgroup_from_task(next, NULL))
1450 __perf_sw_event_sched(PERF_COUNT_SW_CGROUP_SWITCHES, 1, 0);
1453 if (static_branch_unlikely(&perf_sched_events))
1454 __perf_event_task_sched_out(prev, next);
1457 extern void perf_event_mmap(struct vm_area_struct *vma);
1459 extern void perf_event_ksymbol(u16 ksym_type, u64 addr, u32 len,
1460 bool unregister, const char *sym);
1461 extern void perf_event_bpf_event(struct bpf_prog *prog,
1462 enum perf_bpf_event_type type,
1465 #ifdef CONFIG_GUEST_PERF_EVENTS
1466 extern struct perf_guest_info_callbacks __rcu *perf_guest_cbs;
1468 DECLARE_STATIC_CALL(__perf_guest_state, *perf_guest_cbs->state);
1469 DECLARE_STATIC_CALL(__perf_guest_get_ip, *perf_guest_cbs->get_ip);
1470 DECLARE_STATIC_CALL(__perf_guest_handle_intel_pt_intr, *perf_guest_cbs->handle_intel_pt_intr);
1472 static inline unsigned int perf_guest_state(void)
1474 return static_call(__perf_guest_state)();
1476 static inline unsigned long perf_guest_get_ip(void)
1478 return static_call(__perf_guest_get_ip)();
1480 static inline unsigned int perf_guest_handle_intel_pt_intr(void)
1482 return static_call(__perf_guest_handle_intel_pt_intr)();
1484 extern void perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs);
1485 extern void perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs);
1487 static inline unsigned int perf_guest_state(void) { return 0; }
1488 static inline unsigned long perf_guest_get_ip(void) { return 0; }
1489 static inline unsigned int perf_guest_handle_intel_pt_intr(void) { return 0; }
1490 #endif /* CONFIG_GUEST_PERF_EVENTS */
1492 extern void perf_event_exec(void);
1493 extern void perf_event_comm(struct task_struct *tsk, bool exec);
1494 extern void perf_event_namespaces(struct task_struct *tsk);
1495 extern void perf_event_fork(struct task_struct *tsk);
1496 extern void perf_event_text_poke(const void *addr,
1497 const void *old_bytes, size_t old_len,
1498 const void *new_bytes, size_t new_len);
1501 DECLARE_PER_CPU(struct perf_callchain_entry, perf_callchain_entry);
1503 extern void perf_callchain_user(struct perf_callchain_entry_ctx *entry, struct pt_regs *regs);
1504 extern void perf_callchain_kernel(struct perf_callchain_entry_ctx *entry, struct pt_regs *regs);
1505 extern struct perf_callchain_entry *
1506 get_perf_callchain(struct pt_regs *regs, u32 init_nr, bool kernel, bool user,
1507 u32 max_stack, bool crosstask, bool add_mark);
1508 extern int get_callchain_buffers(int max_stack);
1509 extern void put_callchain_buffers(void);
1510 extern struct perf_callchain_entry *get_callchain_entry(int *rctx);
1511 extern void put_callchain_entry(int rctx);
1513 extern int sysctl_perf_event_max_stack;
1514 extern int sysctl_perf_event_max_contexts_per_stack;
1516 static inline int perf_callchain_store_context(struct perf_callchain_entry_ctx *ctx, u64 ip)
1518 if (ctx->contexts < sysctl_perf_event_max_contexts_per_stack) {
1519 struct perf_callchain_entry *entry = ctx->entry;
1520 entry->ip[entry->nr++] = ip;
1524 ctx->contexts_maxed = true;
1525 return -1; /* no more room, stop walking the stack */
1529 static inline int perf_callchain_store(struct perf_callchain_entry_ctx *ctx, u64 ip)
1531 if (ctx->nr < ctx->max_stack && !ctx->contexts_maxed) {
1532 struct perf_callchain_entry *entry = ctx->entry;
1533 entry->ip[entry->nr++] = ip;
1537 return -1; /* no more room, stop walking the stack */
1541 extern int sysctl_perf_event_paranoid;
1542 extern int sysctl_perf_event_mlock;
1543 extern int sysctl_perf_event_sample_rate;
1544 extern int sysctl_perf_cpu_time_max_percent;
1546 extern void perf_sample_event_took(u64 sample_len_ns);
1548 int perf_proc_update_handler(struct ctl_table *table, int write,
1549 void *buffer, size_t *lenp, loff_t *ppos);
1550 int perf_cpu_time_max_percent_handler(struct ctl_table *table, int write,
1551 void *buffer, size_t *lenp, loff_t *ppos);
1552 int perf_event_max_stack_handler(struct ctl_table *table, int write,
1553 void *buffer, size_t *lenp, loff_t *ppos);
1555 /* Access to perf_event_open(2) syscall. */
1556 #define PERF_SECURITY_OPEN 0
1558 /* Finer grained perf_event_open(2) access control. */
1559 #define PERF_SECURITY_CPU 1
1560 #define PERF_SECURITY_KERNEL 2
1561 #define PERF_SECURITY_TRACEPOINT 3
1563 static inline int perf_is_paranoid(void)
1565 return sysctl_perf_event_paranoid > -1;
1568 static inline int perf_allow_kernel(struct perf_event_attr *attr)
1570 if (sysctl_perf_event_paranoid > 1 && !perfmon_capable())
1573 return security_perf_event_open(attr, PERF_SECURITY_KERNEL);
1576 static inline int perf_allow_cpu(struct perf_event_attr *attr)
1578 if (sysctl_perf_event_paranoid > 0 && !perfmon_capable())
1581 return security_perf_event_open(attr, PERF_SECURITY_CPU);
1584 static inline int perf_allow_tracepoint(struct perf_event_attr *attr)
1586 if (sysctl_perf_event_paranoid > -1 && !perfmon_capable())
1589 return security_perf_event_open(attr, PERF_SECURITY_TRACEPOINT);
1592 extern void perf_event_init(void);
1593 extern void perf_tp_event(u16 event_type, u64 count, void *record,
1594 int entry_size, struct pt_regs *regs,
1595 struct hlist_head *head, int rctx,
1596 struct task_struct *task);
1597 extern void perf_bp_event(struct perf_event *event, void *data);
1599 #ifndef perf_misc_flags
1600 # define perf_misc_flags(regs) \
1601 (user_mode(regs) ? PERF_RECORD_MISC_USER : PERF_RECORD_MISC_KERNEL)
1602 # define perf_instruction_pointer(regs) instruction_pointer(regs)
1604 #ifndef perf_arch_bpf_user_pt_regs
1605 # define perf_arch_bpf_user_pt_regs(regs) regs
1608 static inline bool has_branch_stack(struct perf_event *event)
1610 return event->attr.sample_type & PERF_SAMPLE_BRANCH_STACK;
1613 static inline bool needs_branch_stack(struct perf_event *event)
1615 return event->attr.branch_sample_type != 0;
1618 static inline bool has_aux(struct perf_event *event)
1620 return event->pmu->setup_aux;
1623 static inline bool is_write_backward(struct perf_event *event)
1625 return !!event->attr.write_backward;
1628 static inline bool has_addr_filter(struct perf_event *event)
1630 return event->pmu->nr_addr_filters;
1634 * An inherited event uses parent's filters
1636 static inline struct perf_addr_filters_head *
1637 perf_event_addr_filters(struct perf_event *event)
1639 struct perf_addr_filters_head *ifh = &event->addr_filters;
1642 ifh = &event->parent->addr_filters;
1647 extern void perf_event_addr_filters_sync(struct perf_event *event);
1648 extern void perf_report_aux_output_id(struct perf_event *event, u64 hw_id);
1650 extern int perf_output_begin(struct perf_output_handle *handle,
1651 struct perf_sample_data *data,
1652 struct perf_event *event, unsigned int size);
1653 extern int perf_output_begin_forward(struct perf_output_handle *handle,
1654 struct perf_sample_data *data,
1655 struct perf_event *event,
1657 extern int perf_output_begin_backward(struct perf_output_handle *handle,
1658 struct perf_sample_data *data,
1659 struct perf_event *event,
1662 extern void perf_output_end(struct perf_output_handle *handle);
1663 extern unsigned int perf_output_copy(struct perf_output_handle *handle,
1664 const void *buf, unsigned int len);
1665 extern unsigned int perf_output_skip(struct perf_output_handle *handle,
1667 extern long perf_output_copy_aux(struct perf_output_handle *aux_handle,
1668 struct perf_output_handle *handle,
1669 unsigned long from, unsigned long to);
1670 extern int perf_swevent_get_recursion_context(void);
1671 extern void perf_swevent_put_recursion_context(int rctx);
1672 extern u64 perf_swevent_set_period(struct perf_event *event);
1673 extern void perf_event_enable(struct perf_event *event);
1674 extern void perf_event_disable(struct perf_event *event);
1675 extern void perf_event_disable_local(struct perf_event *event);
1676 extern void perf_event_disable_inatomic(struct perf_event *event);
1677 extern void perf_event_task_tick(void);
1678 extern int perf_event_account_interrupt(struct perf_event *event);
1679 extern int perf_event_period(struct perf_event *event, u64 value);
1680 extern u64 perf_event_pause(struct perf_event *event, bool reset);
1681 #else /* !CONFIG_PERF_EVENTS: */
1682 static inline void *
1683 perf_aux_output_begin(struct perf_output_handle *handle,
1684 struct perf_event *event) { return NULL; }
1686 perf_aux_output_end(struct perf_output_handle *handle, unsigned long size)
1689 perf_aux_output_skip(struct perf_output_handle *handle,
1690 unsigned long size) { return -EINVAL; }
1691 static inline void *
1692 perf_get_aux(struct perf_output_handle *handle) { return NULL; }
1694 perf_event_task_migrate(struct task_struct *task) { }
1696 perf_event_task_sched_in(struct task_struct *prev,
1697 struct task_struct *task) { }
1699 perf_event_task_sched_out(struct task_struct *prev,
1700 struct task_struct *next) { }
1701 static inline int perf_event_init_task(struct task_struct *child,
1702 u64 clone_flags) { return 0; }
1703 static inline void perf_event_exit_task(struct task_struct *child) { }
1704 static inline void perf_event_free_task(struct task_struct *task) { }
1705 static inline void perf_event_delayed_put(struct task_struct *task) { }
1706 static inline struct file *perf_event_get(unsigned int fd) { return ERR_PTR(-EINVAL); }
1707 static inline const struct perf_event *perf_get_event(struct file *file)
1709 return ERR_PTR(-EINVAL);
1711 static inline const struct perf_event_attr *perf_event_attrs(struct perf_event *event)
1713 return ERR_PTR(-EINVAL);
1715 static inline int perf_event_read_local(struct perf_event *event, u64 *value,
1716 u64 *enabled, u64 *running)
1720 static inline void perf_event_print_debug(void) { }
1721 static inline int perf_event_task_disable(void) { return -EINVAL; }
1722 static inline int perf_event_task_enable(void) { return -EINVAL; }
1723 static inline int perf_event_refresh(struct perf_event *event, int refresh)
1729 perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr) { }
1731 perf_bp_event(struct perf_event *event, void *data) { }
1733 static inline void perf_event_mmap(struct vm_area_struct *vma) { }
1735 typedef int (perf_ksymbol_get_name_f)(char *name, int name_len, void *data);
1736 static inline void perf_event_ksymbol(u16 ksym_type, u64 addr, u32 len,
1737 bool unregister, const char *sym) { }
1738 static inline void perf_event_bpf_event(struct bpf_prog *prog,
1739 enum perf_bpf_event_type type,
1741 static inline void perf_event_exec(void) { }
1742 static inline void perf_event_comm(struct task_struct *tsk, bool exec) { }
1743 static inline void perf_event_namespaces(struct task_struct *tsk) { }
1744 static inline void perf_event_fork(struct task_struct *tsk) { }
1745 static inline void perf_event_text_poke(const void *addr,
1746 const void *old_bytes,
1748 const void *new_bytes,
1750 static inline void perf_event_init(void) { }
1751 static inline int perf_swevent_get_recursion_context(void) { return -1; }
1752 static inline void perf_swevent_put_recursion_context(int rctx) { }
1753 static inline u64 perf_swevent_set_period(struct perf_event *event) { return 0; }
1754 static inline void perf_event_enable(struct perf_event *event) { }
1755 static inline void perf_event_disable(struct perf_event *event) { }
1756 static inline int __perf_event_disable(void *info) { return -1; }
1757 static inline void perf_event_task_tick(void) { }
1758 static inline int perf_event_release_kernel(struct perf_event *event) { return 0; }
1759 static inline int perf_event_period(struct perf_event *event, u64 value)
1763 static inline u64 perf_event_pause(struct perf_event *event, bool reset)
1769 #if defined(CONFIG_PERF_EVENTS) && defined(CONFIG_CPU_SUP_INTEL)
1770 extern void perf_restore_debug_store(void);
1772 static inline void perf_restore_debug_store(void) { }
1775 #define perf_output_put(handle, x) perf_output_copy((handle), &(x), sizeof(x))
1777 struct perf_pmu_events_attr {
1778 struct device_attribute attr;
1780 const char *event_str;
1783 struct perf_pmu_events_ht_attr {
1784 struct device_attribute attr;
1786 const char *event_str_ht;
1787 const char *event_str_noht;
1790 struct perf_pmu_events_hybrid_attr {
1791 struct device_attribute attr;
1793 const char *event_str;
1797 struct perf_pmu_format_hybrid_attr {
1798 struct device_attribute attr;
1802 ssize_t perf_event_sysfs_show(struct device *dev, struct device_attribute *attr,
1805 #define PMU_EVENT_ATTR(_name, _var, _id, _show) \
1806 static struct perf_pmu_events_attr _var = { \
1807 .attr = __ATTR(_name, 0444, _show, NULL), \
1811 #define PMU_EVENT_ATTR_STRING(_name, _var, _str) \
1812 static struct perf_pmu_events_attr _var = { \
1813 .attr = __ATTR(_name, 0444, perf_event_sysfs_show, NULL), \
1815 .event_str = _str, \
1818 #define PMU_EVENT_ATTR_ID(_name, _show, _id) \
1819 (&((struct perf_pmu_events_attr[]) { \
1820 { .attr = __ATTR(_name, 0444, _show, NULL), \
1824 #define PMU_FORMAT_ATTR_SHOW(_name, _format) \
1826 _name##_show(struct device *dev, \
1827 struct device_attribute *attr, \
1830 BUILD_BUG_ON(sizeof(_format) >= PAGE_SIZE); \
1831 return sprintf(page, _format "\n"); \
1834 #define PMU_FORMAT_ATTR(_name, _format) \
1835 PMU_FORMAT_ATTR_SHOW(_name, _format) \
1837 static struct device_attribute format_attr_##_name = __ATTR_RO(_name)
1839 /* Performance counter hotplug functions */
1840 #ifdef CONFIG_PERF_EVENTS
1841 int perf_event_init_cpu(unsigned int cpu);
1842 int perf_event_exit_cpu(unsigned int cpu);
1844 #define perf_event_init_cpu NULL
1845 #define perf_event_exit_cpu NULL
1848 extern void __weak arch_perf_update_userpage(struct perf_event *event,
1849 struct perf_event_mmap_page *userpg,
1853 extern __weak u64 arch_perf_get_page_size(struct mm_struct *mm, unsigned long addr);
1857 * Snapshot branch stack on software events.
1859 * Branch stack can be very useful in understanding software events. For
1860 * example, when a long function, e.g. sys_perf_event_open, returns an
1861 * errno, it is not obvious why the function failed. Branch stack could
1862 * provide very helpful information in this type of scenarios.
1864 * On software event, it is necessary to stop the hardware branch recorder
1865 * fast. Otherwise, the hardware register/buffer will be flushed with
1866 * entries of the triggering event. Therefore, static call is used to
1867 * stop the hardware recorder.
1871 * cnt is the number of entries allocated for entries.
1872 * Return number of entries copied to .
1874 typedef int (perf_snapshot_branch_stack_t)(struct perf_branch_entry *entries,
1876 DECLARE_STATIC_CALL(perf_snapshot_branch_stack, perf_snapshot_branch_stack_t);
1878 #ifndef PERF_NEEDS_LOPWR_CB
1879 static inline void perf_lopwr_cb(bool mode)
1884 #endif /* _LINUX_PERF_EVENT_H */