1 /*
2 * Performance events:
3 *
4 * Copyright (C) 2008-2009, Thomas Gleixner <tglx@linutronix.de>
5 * Copyright (C) 2008-2011, Red Hat, Inc., Ingo Molnar
6 * Copyright (C) 2008-2011, Red Hat, Inc., Peter Zijlstra
7 *
8 * Data type definitions, declarations, prototypes.
9 *
10 * Started by: Thomas Gleixner and Ingo Molnar
11 *
12 * For licencing details see kernel-base/COPYING
13 */
14 #ifndef _LINUX_PERF_EVENT_H
15 #define _LINUX_PERF_EVENT_H
16
17 #include <uapi/linux/perf_event.h>
18 #include <uapi/linux/bpf_perf_event.h>
19
20 /*
21 * Kernel-internal data types and definitions:
22 */
23
24 #ifdef CONFIG_PERF_EVENTS
25 # include <asm/perf_event.h>
26 # include <asm/local64.h>
27 #endif
28
29 #define PERF_GUEST_ACTIVE 0x01
30 #define PERF_GUEST_USER 0x02
31
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);
36 };
37
38 #ifdef CONFIG_HAVE_HW_BREAKPOINT
39 #include <linux/rhashtable-types.h>
40 #include <asm/hw_breakpoint.h>
41 #endif
42
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>
49 #include <linux/fs.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>
66
67 struct perf_callchain_entry {
68 __u64 nr;
69 __u64 ip[]; /* /proc/sys/kernel/perf_event_max_stack */
70 };
71
72 struct perf_callchain_entry_ctx {
73 struct perf_callchain_entry *entry;
74 u32 max_stack;
75 u32 nr;
76 short contexts;
77 bool contexts_maxed;
78 };
79
80 typedef unsigned long (*perf_copy_f)(void *dst, const void *src,
81 unsigned long off, unsigned long len);
82
83 struct perf_raw_frag {
84 union {
85 struct perf_raw_frag *next;
86 unsigned long pad;
87 };
88 perf_copy_f copy;
89 void *data;
90 u32 size;
91 } __packed;
92
93 struct perf_raw_record {
94 struct perf_raw_frag frag;
95 u32 size;
96 };
97
98 /*
99 * branch stack layout:
100 * nr: number of taken branches stored in entries[]
101 * hw_idx: The low level index of raw branch records
102 * for the most recent branch.
103 * -1ULL means invalid/unknown.
104 *
105 * Note that nr can vary from sample to sample
106 * branches (to, from) are stored from most recent
107 * to least recent, i.e., entries[0] contains the most
108 * recent branch.
109 * The entries[] is an abstraction of raw branch records,
110 * which may not be stored in age order in HW, e.g. Intel LBR.
111 * The hw_idx is to expose the low level index of raw
112 * branch record for the most recent branch aka entries[0].
113 * The hw_idx index is between -1 (unknown) and max depth,
114 * which can be retrieved in /sys/devices/cpu/caps/branches.
115 * For the architectures whose raw branch records are
116 * already stored in age order, the hw_idx should be 0.
117 */
118 struct perf_branch_stack {
119 __u64 nr;
120 __u64 hw_idx;
121 struct perf_branch_entry entries[];
122 };
123
124 struct task_struct;
125
126 /*
127 * extra PMU register associated with an event
128 */
129 struct hw_perf_event_extra {
130 u64 config; /* register value */
131 unsigned int reg; /* register address or index */
132 int alloc; /* extra register already allocated */
133 int idx; /* index in shared_regs->regs[] */
134 };
135
136 /**
137 * hw_perf_event::flag values
138 *
139 * PERF_EVENT_FLAG_ARCH bits are reserved for architecture-specific
140 * usage.
141 */
142 #define PERF_EVENT_FLAG_ARCH 0x000fffff
143 #define PERF_EVENT_FLAG_USER_READ_CNT 0x80000000
144
145 static_assert((PERF_EVENT_FLAG_USER_READ_CNT & PERF_EVENT_FLAG_ARCH) == 0);
146
147 /**
148 * struct hw_perf_event - performance event hardware details:
149 */
150 struct hw_perf_event {
151 #ifdef CONFIG_PERF_EVENTS
152 union {
153 struct { /* hardware */
154 u64 config;
155 u64 last_tag;
156 unsigned long config_base;
157 unsigned long event_base;
158 int event_base_rdpmc;
159 int idx;
160 int last_cpu;
161 int flags;
162
163 struct hw_perf_event_extra extra_reg;
164 struct hw_perf_event_extra branch_reg;
165 };
166 struct { /* software */
167 struct hrtimer hrtimer;
168 };
169 struct { /* tracepoint */
170 /* for tp_event->class */
171 struct list_head tp_list;
172 };
173 struct { /* amd_power */
174 u64 pwr_acc;
175 u64 ptsc;
176 };
177 #ifdef CONFIG_HAVE_HW_BREAKPOINT
178 struct { /* breakpoint */
179 /*
180 * Crufty hack to avoid the chicken and egg
181 * problem hw_breakpoint has with context
182 * creation and event initalization.
183 */
184 struct arch_hw_breakpoint info;
185 struct rhlist_head bp_list;
186 };
187 #endif
188 struct { /* amd_iommu */
189 u8 iommu_bank;
190 u8 iommu_cntr;
191 u16 padding;
192 u64 conf;
193 u64 conf1;
194 };
195 };
196 /*
197 * If the event is a per task event, this will point to the task in
198 * question. See the comment in perf_event_alloc().
199 */
200 struct task_struct *target;
201
202 /*
203 * PMU would store hardware filter configuration
204 * here.
205 */
206 void *addr_filters;
207
208 /* Last sync'ed generation of filters */
209 unsigned long addr_filters_gen;
210
211 /*
212 * hw_perf_event::state flags; used to track the PERF_EF_* state.
213 */
214 #define PERF_HES_STOPPED 0x01 /* the counter is stopped */
215 #define PERF_HES_UPTODATE 0x02 /* event->count up-to-date */
216 #define PERF_HES_ARCH 0x04
217
218 int state;
219
220 /*
221 * The last observed hardware counter value, updated with a
222 * local64_cmpxchg() such that pmu::read() can be called nested.
223 */
224 local64_t prev_count;
225
226 /*
227 * The period to start the next sample with.
228 */
229 u64 sample_period;
230
231 union {
232 struct { /* Sampling */
233 /*
234 * The period we started this sample with.
235 */
236 u64 last_period;
237
238 /*
239 * However much is left of the current period;
240 * note that this is a full 64bit value and
241 * allows for generation of periods longer
242 * than hardware might allow.
243 */
244 local64_t period_left;
245 };
246 struct { /* Topdown events counting for context switch */
247 u64 saved_metric;
248 u64 saved_slots;
249 };
250 };
251
252 /*
253 * State for throttling the event, see __perf_event_overflow() and
254 * perf_adjust_freq_unthr_context().
255 */
256 u64 interrupts_seq;
257 u64 interrupts;
258
259 /*
260 * State for freq target events, see __perf_event_overflow() and
261 * perf_adjust_freq_unthr_context().
262 */
263 u64 freq_time_stamp;
264 u64 freq_count_stamp;
265 #endif
266 };
267
268 struct perf_event;
269
270 /*
271 * Common implementation detail of pmu::{start,commit,cancel}_txn
272 */
273 #define PERF_PMU_TXN_ADD 0x1 /* txn to add/schedule event on PMU */
274 #define PERF_PMU_TXN_READ 0x2 /* txn to read event group from PMU */
275
276 /**
277 * pmu::capabilities flags
278 */
279 #define PERF_PMU_CAP_NO_INTERRUPT 0x0001
280 #define PERF_PMU_CAP_NO_NMI 0x0002
281 #define PERF_PMU_CAP_AUX_NO_SG 0x0004
282 #define PERF_PMU_CAP_EXTENDED_REGS 0x0008
283 #define PERF_PMU_CAP_EXCLUSIVE 0x0010
284 #define PERF_PMU_CAP_ITRACE 0x0020
285 #define PERF_PMU_CAP_HETEROGENEOUS_CPUS 0x0040
286 #define PERF_PMU_CAP_NO_EXCLUDE 0x0080
287 #define PERF_PMU_CAP_AUX_OUTPUT 0x0100
288 #define PERF_PMU_CAP_EXTENDED_HW_TYPE 0x0200
289
290 struct perf_output_handle;
291
292 /**
293 * struct pmu - generic performance monitoring unit
294 */
295 struct pmu {
296 struct list_head entry;
297
298 struct module *module;
299 struct device *dev;
300 const struct attribute_group **attr_groups;
301 const struct attribute_group **attr_update;
302 const char *name;
303 int type;
304
305 /*
306 * various common per-pmu feature flags
307 */
308 int capabilities;
309
310 int __percpu *pmu_disable_count;
311 struct perf_cpu_context __percpu *pmu_cpu_context;
312 atomic_t exclusive_cnt; /* < 0: cpu; > 0: tsk */
313 int task_ctx_nr;
314 int hrtimer_interval_ms;
315
316 /* number of address filters this PMU can do */
317 unsigned int nr_addr_filters;
318
319 /*
320 * Fully disable/enable this PMU, can be used to protect from the PMI
321 * as well as for lazy/batch writing of the MSRs.
322 */
323 void (*pmu_enable) (struct pmu *pmu); /* optional */
324 void (*pmu_disable) (struct pmu *pmu); /* optional */
325
326 /*
327 * Try and initialize the event for this PMU.
328 *
329 * Returns:
330 * -ENOENT -- @event is not for this PMU
331 *
332 * -ENODEV -- @event is for this PMU but PMU not present
333 * -EBUSY -- @event is for this PMU but PMU temporarily unavailable
334 * -EINVAL -- @event is for this PMU but @event is not valid
335 * -EOPNOTSUPP -- @event is for this PMU, @event is valid, but not supported
336 * -EACCES -- @event is for this PMU, @event is valid, but no privileges
337 *
338 * 0 -- @event is for this PMU and valid
339 *
340 * Other error return values are allowed.
341 */
342 int (*event_init) (struct perf_event *event);
343
344 /*
345 * Notification that the event was mapped or unmapped. Called
346 * in the context of the mapping task.
347 */
348 void (*event_mapped) (struct perf_event *event, struct mm_struct *mm); /* optional */
349 void (*event_unmapped) (struct perf_event *event, struct mm_struct *mm); /* optional */
350
351 /*
352 * Flags for ->add()/->del()/ ->start()/->stop(). There are
353 * matching hw_perf_event::state flags.
354 */
355 #define PERF_EF_START 0x01 /* start the counter when adding */
356 #define PERF_EF_RELOAD 0x02 /* reload the counter when starting */
357 #define PERF_EF_UPDATE 0x04 /* update the counter when stopping */
358
359 /*
360 * Adds/Removes a counter to/from the PMU, can be done inside a
361 * transaction, see the ->*_txn() methods.
362 *
363 * The add/del callbacks will reserve all hardware resources required
364 * to service the event, this includes any counter constraint
365 * scheduling etc.
366 *
367 * Called with IRQs disabled and the PMU disabled on the CPU the event
368 * is on.
369 *
370 * ->add() called without PERF_EF_START should result in the same state
371 * as ->add() followed by ->stop().
372 *
373 * ->del() must always PERF_EF_UPDATE stop an event. If it calls
374 * ->stop() that must deal with already being stopped without
375 * PERF_EF_UPDATE.
376 */
377 int (*add) (struct perf_event *event, int flags);
378 void (*del) (struct perf_event *event, int flags);
379
380 /*
381 * Starts/Stops a counter present on the PMU.
382 *
383 * The PMI handler should stop the counter when perf_event_overflow()
384 * returns !0. ->start() will be used to continue.
385 *
386 * Also used to change the sample period.
387 *
388 * Called with IRQs disabled and the PMU disabled on the CPU the event
389 * is on -- will be called from NMI context with the PMU generates
390 * NMIs.
391 *
392 * ->stop() with PERF_EF_UPDATE will read the counter and update
393 * period/count values like ->read() would.
394 *
395 * ->start() with PERF_EF_RELOAD will reprogram the counter
396 * value, must be preceded by a ->stop() with PERF_EF_UPDATE.
397 */
398 void (*start) (struct perf_event *event, int flags);
399 void (*stop) (struct perf_event *event, int flags);
400
401 /*
402 * Updates the counter value of the event.
403 *
404 * For sampling capable PMUs this will also update the software period
405 * hw_perf_event::period_left field.
406 */
407 void (*read) (struct perf_event *event);
408
409 /*
410 * Group events scheduling is treated as a transaction, add
411 * group events as a whole and perform one schedulability test.
412 * If the test fails, roll back the whole group
413 *
414 * Start the transaction, after this ->add() doesn't need to
415 * do schedulability tests.
416 *
417 * Optional.
418 */
419 void (*start_txn) (struct pmu *pmu, unsigned int txn_flags);
420 /*
421 * If ->start_txn() disabled the ->add() schedulability test
422 * then ->commit_txn() is required to perform one. On success
423 * the transaction is closed. On error the transaction is kept
424 * open until ->cancel_txn() is called.
425 *
426 * Optional.
427 */
428 int (*commit_txn) (struct pmu *pmu);
429 /*
430 * Will cancel the transaction, assumes ->del() is called
431 * for each successful ->add() during the transaction.
432 *
433 * Optional.
434 */
435 void (*cancel_txn) (struct pmu *pmu);
436
437 /*
438 * Will return the value for perf_event_mmap_page::index for this event,
439 * if no implementation is provided it will default to: event->hw.idx + 1.
440 */
441 int (*event_idx) (struct perf_event *event); /*optional */
442
443 /*
444 * context-switches callback
445 */
446 void (*sched_task) (struct perf_event_context *ctx,
447 bool sched_in);
448
449 /*
450 * Kmem cache of PMU specific data
451 */
452 struct kmem_cache *task_ctx_cache;
453
454 /*
455 * PMU specific parts of task perf event context (i.e. ctx->task_ctx_data)
456 * can be synchronized using this function. See Intel LBR callstack support
457 * implementation and Perf core context switch handling callbacks for usage
458 * examples.
459 */
460 void (*swap_task_ctx) (struct perf_event_context *prev,
461 struct perf_event_context *next);
462 /* optional */
463
464 /*
465 * Set up pmu-private data structures for an AUX area
466 */
467 void *(*setup_aux) (struct perf_event *event, void **pages,
468 int nr_pages, bool overwrite);
469 /* optional */
470
471 /*
472 * Free pmu-private AUX data structures
473 */
474 void (*free_aux) (void *aux); /* optional */
475
476 /*
477 * Take a snapshot of the AUX buffer without touching the event
478 * state, so that preempting ->start()/->stop() callbacks does
479 * not interfere with their logic. Called in PMI context.
480 *
481 * Returns the size of AUX data copied to the output handle.
482 *
483 * Optional.
484 */
485 long (*snapshot_aux) (struct perf_event *event,
486 struct perf_output_handle *handle,
487 unsigned long size);
488
489 /*
490 * Validate address range filters: make sure the HW supports the
491 * requested configuration and number of filters; return 0 if the
492 * supplied filters are valid, -errno otherwise.
493 *
494 * Runs in the context of the ioctl()ing process and is not serialized
495 * with the rest of the PMU callbacks.
496 */
497 int (*addr_filters_validate) (struct list_head *filters);
498 /* optional */
499
500 /*
501 * Synchronize address range filter configuration:
502 * translate hw-agnostic filters into hardware configuration in
503 * event::hw::addr_filters.
504 *
505 * Runs as a part of filter sync sequence that is done in ->start()
506 * callback by calling perf_event_addr_filters_sync().
507 *
508 * May (and should) traverse event::addr_filters::list, for which its
509 * caller provides necessary serialization.
510 */
511 void (*addr_filters_sync) (struct perf_event *event);
512 /* optional */
513
514 /*
515 * Check if event can be used for aux_output purposes for
516 * events of this PMU.
517 *
518 * Runs from perf_event_open(). Should return 0 for "no match"
519 * or non-zero for "match".
520 */
521 int (*aux_output_match) (struct perf_event *event);
522 /* optional */
523
524 /*
525 * Filter events for PMU-specific reasons.
526 */
527 int (*filter_match) (struct perf_event *event); /* optional */
528
529 /*
530 * Check period value for PERF_EVENT_IOC_PERIOD ioctl.
531 */
532 int (*check_period) (struct perf_event *event, u64 value); /* optional */
533 };
534
535 enum perf_addr_filter_action_t {
536 PERF_ADDR_FILTER_ACTION_STOP = 0,
537 PERF_ADDR_FILTER_ACTION_START,
538 PERF_ADDR_FILTER_ACTION_FILTER,
539 };
540
541 /**
542 * struct perf_addr_filter - address range filter definition
543 * @entry: event's filter list linkage
544 * @path: object file's path for file-based filters
545 * @offset: filter range offset
546 * @size: filter range size (size==0 means single address trigger)
547 * @action: filter/start/stop
548 *
549 * This is a hardware-agnostic filter configuration as specified by the user.
550 */
551 struct perf_addr_filter {
552 struct list_head entry;
553 struct path path;
554 unsigned long offset;
555 unsigned long size;
556 enum perf_addr_filter_action_t action;
557 };
558
559 /**
560 * struct perf_addr_filters_head - container for address range filters
561 * @list: list of filters for this event
562 * @lock: spinlock that serializes accesses to the @list and event's
563 * (and its children's) filter generations.
564 * @nr_file_filters: number of file-based filters
565 *
566 * A child event will use parent's @list (and therefore @lock), so they are
567 * bundled together; see perf_event_addr_filters().
568 */
569 struct perf_addr_filters_head {
570 struct list_head list;
571 raw_spinlock_t lock;
572 unsigned int nr_file_filters;
573 };
574
575 struct perf_addr_filter_range {
576 unsigned long start;
577 unsigned long size;
578 };
579
580 /**
581 * enum perf_event_state - the states of an event:
582 */
583 enum perf_event_state {
584 PERF_EVENT_STATE_DEAD = -4,
585 PERF_EVENT_STATE_EXIT = -3,
586 PERF_EVENT_STATE_ERROR = -2,
587 PERF_EVENT_STATE_OFF = -1,
588 PERF_EVENT_STATE_INACTIVE = 0,
589 PERF_EVENT_STATE_ACTIVE = 1,
590 };
591
592 struct file;
593 struct perf_sample_data;
594
595 typedef void (*perf_overflow_handler_t)(struct perf_event *,
596 struct perf_sample_data *,
597 struct pt_regs *regs);
598
599 /*
600 * Event capabilities. For event_caps and groups caps.
601 *
602 * PERF_EV_CAP_SOFTWARE: Is a software event.
603 * PERF_EV_CAP_READ_ACTIVE_PKG: A CPU event (or cgroup event) that can be read
604 * from any CPU in the package where it is active.
605 * PERF_EV_CAP_SIBLING: An event with this flag must be a group sibling and
606 * cannot be a group leader. If an event with this flag is detached from the
607 * group it is scheduled out and moved into an unrecoverable ERROR state.
608 */
609 #define PERF_EV_CAP_SOFTWARE BIT(0)
610 #define PERF_EV_CAP_READ_ACTIVE_PKG BIT(1)
611 #define PERF_EV_CAP_SIBLING BIT(2)
612
613 #define SWEVENT_HLIST_BITS 8
614 #define SWEVENT_HLIST_SIZE (1 << SWEVENT_HLIST_BITS)
615
616 struct swevent_hlist {
617 struct hlist_head heads[SWEVENT_HLIST_SIZE];
618 struct rcu_head rcu_head;
619 };
620
621 #define PERF_ATTACH_CONTEXT 0x01
622 #define PERF_ATTACH_GROUP 0x02
623 #define PERF_ATTACH_TASK 0x04
624 #define PERF_ATTACH_TASK_DATA 0x08
625 #define PERF_ATTACH_ITRACE 0x10
626 #define PERF_ATTACH_SCHED_CB 0x20
627 #define PERF_ATTACH_CHILD 0x40
628
629 struct bpf_prog;
630 struct perf_cgroup;
631 struct perf_buffer;
632
633 struct pmu_event_list {
634 raw_spinlock_t lock;
635 struct list_head list;
636 };
637
638 /*
639 * event->sibling_list is modified whole holding both ctx->lock and ctx->mutex
640 * as such iteration must hold either lock. However, since ctx->lock is an IRQ
641 * safe lock, and is only held by the CPU doing the modification, having IRQs
642 * disabled is sufficient since it will hold-off the IPIs.
643 */
644 #ifdef CONFIG_PROVE_LOCKING
645 #define lockdep_assert_event_ctx(event) \
646 WARN_ON_ONCE(__lockdep_enabled && \
647 (this_cpu_read(hardirqs_enabled) && \
648 lockdep_is_held(&(event)->ctx->mutex) != LOCK_STATE_HELD))
649 #else
650 #define lockdep_assert_event_ctx(event)
651 #endif
652
653 #define for_each_sibling_event(sibling, event) \
654 lockdep_assert_event_ctx(event); \
655 if ((event)->group_leader == (event)) \
656 list_for_each_entry((sibling), &(event)->sibling_list, sibling_list)
657
658 /**
659 * struct perf_event - performance event kernel representation:
660 */
661 struct perf_event {
662 #ifdef CONFIG_PERF_EVENTS
663 /*
664 * entry onto perf_event_context::event_list;
665 * modifications require ctx->lock
666 * RCU safe iterations.
667 */
668 struct list_head event_entry;
669
670 /*
671 * Locked for modification by both ctx->mutex and ctx->lock; holding
672 * either sufficies for read.
673 */
674 struct list_head sibling_list;
675 struct list_head active_list;
676 /*
677 * Node on the pinned or flexible tree located at the event context;
678 */
679 struct rb_node group_node;
680 u64 group_index;
681 /*
682 * We need storage to track the entries in perf_pmu_migrate_context; we
683 * cannot use the event_entry because of RCU and we want to keep the
684 * group in tact which avoids us using the other two entries.
685 */
686 struct list_head migrate_entry;
687
688 struct hlist_node hlist_entry;
689 struct list_head active_entry;
690 int nr_siblings;
691
692 /* Not serialized. Only written during event initialization. */
693 int event_caps;
694 /* The cumulative AND of all event_caps for events in this group. */
695 int group_caps;
696
697 #ifndef __GENKSYMS__
698 unsigned int group_generation;
699 #endif
700 struct perf_event *group_leader;
701 struct pmu *pmu;
702 void *pmu_private;
703
704 enum perf_event_state state;
705 unsigned int attach_state;
706 local64_t count;
707 atomic64_t child_count;
708
709 /*
710 * These are the total time in nanoseconds that the event
711 * has been enabled (i.e. eligible to run, and the task has
712 * been scheduled in, if this is a per-task event)
713 * and running (scheduled onto the CPU), respectively.
714 */
715 u64 total_time_enabled;
716 u64 total_time_running;
717 u64 tstamp;
718
719 struct perf_event_attr attr;
720 u16 header_size;
721 u16 id_header_size;
722 u16 read_size;
723 struct hw_perf_event hw;
724
725 struct perf_event_context *ctx;
726 atomic_long_t refcount;
727
728 /*
729 * These accumulate total time (in nanoseconds) that children
730 * events have been enabled and running, respectively.
731 */
732 atomic64_t child_total_time_enabled;
733 atomic64_t child_total_time_running;
734
735 /*
736 * Protect attach/detach and child_list:
737 */
738 struct mutex child_mutex;
739 struct list_head child_list;
740 struct perf_event *parent;
741
742 int oncpu;
743 int cpu;
744
745 struct list_head owner_entry;
746 struct task_struct *owner;
747
748 /* mmap bits */
749 struct mutex mmap_mutex;
750 atomic_t mmap_count;
751
752 struct perf_buffer *rb;
753 struct list_head rb_entry;
754 unsigned long rcu_batches;
755 int rcu_pending;
756
757 /* poll related */
758 wait_queue_head_t waitq;
759 struct fasync_struct *fasync;
760
761 /* delayed work for NMIs and such */
762 unsigned int pending_wakeup;
763 unsigned int pending_kill;
764 unsigned int pending_disable;
765 unsigned int pending_sigtrap;
766 unsigned long pending_addr; /* SIGTRAP */
767 struct irq_work pending_irq;
768 struct callback_head pending_task;
769 unsigned int pending_work;
770
771 atomic_t event_limit;
772
773 /* address range filters */
774 struct perf_addr_filters_head addr_filters;
775 /* vma address array for file-based filders */
776 struct perf_addr_filter_range *addr_filter_ranges;
777 unsigned long addr_filters_gen;
778
779 /* for aux_output events */
780 struct perf_event *aux_event;
781
782 void (*destroy)(struct perf_event *);
783 struct rcu_head rcu_head;
784
785 struct pid_namespace *ns;
786 u64 id;
787
788 atomic64_t lost_samples;
789
790 u64 (*clock)(void);
791 perf_overflow_handler_t overflow_handler;
792 void *overflow_handler_context;
793 #ifdef CONFIG_BPF_SYSCALL
794 perf_overflow_handler_t orig_overflow_handler;
795 struct bpf_prog *prog;
796 u64 bpf_cookie;
797 #endif
798
799 #ifdef CONFIG_EVENT_TRACING
800 struct trace_event_call *tp_event;
801 struct event_filter *filter;
802 #ifdef CONFIG_FUNCTION_TRACER
803 struct ftrace_ops ftrace_ops;
804 #endif
805 #endif
806
807 #ifdef CONFIG_CGROUP_PERF
808 struct perf_cgroup *cgrp; /* cgroup event is attach to */
809 #endif
810
811 #ifdef CONFIG_SECURITY
812 void *security;
813 #endif
814 struct list_head sb_list;
815 #endif /* CONFIG_PERF_EVENTS */
816 };
817
818
819 struct perf_event_groups {
820 struct rb_root tree;
821 u64 index;
822 };
823
824 /**
825 * struct perf_event_context - event context structure
826 *
827 * Used as a container for task events and CPU events as well:
828 */
829 struct perf_event_context {
830 struct pmu *pmu;
831 /*
832 * Protect the states of the events in the list,
833 * nr_active, and the list:
834 */
835 raw_spinlock_t lock;
836 /*
837 * Protect the list of events. Locking either mutex or lock
838 * is sufficient to ensure the list doesn't change; to change
839 * the list you need to lock both the mutex and the spinlock.
840 */
841 struct mutex mutex;
842
843 struct list_head active_ctx_list;
844 struct perf_event_groups pinned_groups;
845 struct perf_event_groups flexible_groups;
846 struct list_head event_list;
847
848 struct list_head pinned_active;
849 struct list_head flexible_active;
850
851 int nr_events;
852 int nr_active;
853 int nr_user;
854 int is_active;
855 int nr_stat;
856 int nr_freq;
857 int rotate_disable;
858 /*
859 * Set when nr_events != nr_active, except tolerant to events not
860 * necessary to be active due to scheduling constraints, such as cgroups.
861 */
862 int rotate_necessary;
863 refcount_t refcount;
864 struct task_struct *task;
865
866 /*
867 * Context clock, runs when context enabled.
868 */
869 u64 time;
870 u64 timestamp;
871 u64 timeoffset;
872
873 /*
874 * These fields let us detect when two contexts have both
875 * been cloned (inherited) from a common ancestor.
876 */
877 struct perf_event_context *parent_ctx;
878 u64 parent_gen;
879 u64 generation;
880 int pin_count;
881 #ifdef CONFIG_CGROUP_PERF
882 int nr_cgroups; /* cgroup evts */
883 #endif
884 void *task_ctx_data; /* pmu specific data */
885 struct rcu_head rcu_head;
886
887 /*
888 * Sum (event->pending_sigtrap + event->pending_work)
889 *
890 * The SIGTRAP is targeted at ctx->task, as such it won't do changing
891 * that until the signal is delivered.
892 */
893 local_t nr_pending;
894 };
895
896 /*
897 * Number of contexts where an event can trigger:
898 * task, softirq, hardirq, nmi.
899 */
900 #define PERF_NR_CONTEXTS 4
901
902 /**
903 * struct perf_cpu_context - per cpu event context structure
904 */
905 struct perf_cpu_context {
906 struct perf_event_context ctx;
907 struct perf_event_context *task_ctx;
908 int active_oncpu;
909 int exclusive;
910
911 raw_spinlock_t hrtimer_lock;
912 struct hrtimer hrtimer;
913 ktime_t hrtimer_interval;
914 unsigned int hrtimer_active;
915
916 #ifdef CONFIG_CGROUP_PERF
917 struct perf_cgroup *cgrp;
918 struct list_head cgrp_cpuctx_entry;
919 #endif
920
921 struct list_head sched_cb_entry;
922 int sched_cb_usage;
923
924 int online;
925 /*
926 * Per-CPU storage for iterators used in visit_groups_merge. The default
927 * storage is of size 2 to hold the CPU and any CPU event iterators.
928 */
929 int heap_size;
930 struct perf_event **heap;
931 struct perf_event *heap_default[2];
932 };
933
934 struct perf_output_handle {
935 struct perf_event *event;
936 struct perf_buffer *rb;
937 unsigned long wakeup;
938 unsigned long size;
939 u64 aux_flags;
940 union {
941 void *addr;
942 unsigned long head;
943 };
944 int page;
945 };
946
947 struct bpf_perf_event_data_kern {
948 bpf_user_pt_regs_t *regs;
949 struct perf_sample_data *data;
950 struct perf_event *event;
951 };
952
953 #ifdef CONFIG_CGROUP_PERF
954
955 /*
956 * perf_cgroup_info keeps track of time_enabled for a cgroup.
957 * This is a per-cpu dynamically allocated data structure.
958 */
959 struct perf_cgroup_info {
960 u64 time;
961 u64 timestamp;
962 u64 timeoffset;
963 int active;
964 };
965
966 struct perf_cgroup {
967 struct cgroup_subsys_state css;
968 struct perf_cgroup_info __percpu *info;
969 };
970
971 /*
972 * Must ensure cgroup is pinned (css_get) before calling
973 * this function. In other words, we cannot call this function
974 * if there is no cgroup event for the current CPU context.
975 */
976 static inline struct perf_cgroup *
perf_cgroup_from_task(struct task_struct * task,struct perf_event_context * ctx)977 perf_cgroup_from_task(struct task_struct *task, struct perf_event_context *ctx)
978 {
979 return container_of(task_css_check(task, perf_event_cgrp_id,
980 ctx ? lockdep_is_held(&ctx->lock)
981 : true),
982 struct perf_cgroup, css);
983 }
984 #endif /* CONFIG_CGROUP_PERF */
985
986 #ifdef CONFIG_PERF_EVENTS
987
988 extern void *perf_aux_output_begin(struct perf_output_handle *handle,
989 struct perf_event *event);
990 extern void perf_aux_output_end(struct perf_output_handle *handle,
991 unsigned long size);
992 extern int perf_aux_output_skip(struct perf_output_handle *handle,
993 unsigned long size);
994 extern void *perf_get_aux(struct perf_output_handle *handle);
995 extern void perf_aux_output_flag(struct perf_output_handle *handle, u64 flags);
996 extern void perf_event_itrace_started(struct perf_event *event);
997
998 extern int perf_pmu_register(struct pmu *pmu, const char *name, int type);
999 extern void perf_pmu_unregister(struct pmu *pmu);
1000
1001 extern void __perf_event_task_sched_in(struct task_struct *prev,
1002 struct task_struct *task);
1003 extern void __perf_event_task_sched_out(struct task_struct *prev,
1004 struct task_struct *next);
1005 extern int perf_event_init_task(struct task_struct *child, u64 clone_flags);
1006 extern void perf_event_exit_task(struct task_struct *child);
1007 extern void perf_event_free_task(struct task_struct *task);
1008 extern void perf_event_delayed_put(struct task_struct *task);
1009 extern struct file *perf_event_get(unsigned int fd);
1010 extern const struct perf_event *perf_get_event(struct file *file);
1011 extern const struct perf_event_attr *perf_event_attrs(struct perf_event *event);
1012 extern void perf_event_print_debug(void);
1013 extern void perf_pmu_disable(struct pmu *pmu);
1014 extern void perf_pmu_enable(struct pmu *pmu);
1015 extern void perf_sched_cb_dec(struct pmu *pmu);
1016 extern void perf_sched_cb_inc(struct pmu *pmu);
1017 extern int perf_event_task_disable(void);
1018 extern int perf_event_task_enable(void);
1019
1020 extern void perf_pmu_resched(struct pmu *pmu);
1021
1022 extern int perf_event_refresh(struct perf_event *event, int refresh);
1023 extern void perf_event_update_userpage(struct perf_event *event);
1024 extern int perf_event_release_kernel(struct perf_event *event);
1025 extern struct perf_event *
1026 perf_event_create_kernel_counter(struct perf_event_attr *attr,
1027 int cpu,
1028 struct task_struct *task,
1029 perf_overflow_handler_t callback,
1030 void *context);
1031 extern void perf_pmu_migrate_context(struct pmu *pmu,
1032 int src_cpu, int dst_cpu);
1033 int perf_event_read_local(struct perf_event *event, u64 *value,
1034 u64 *enabled, u64 *running);
1035 extern u64 perf_event_read_value(struct perf_event *event,
1036 u64 *enabled, u64 *running);
1037
1038
1039 struct perf_sample_data {
1040 /*
1041 * Fields set by perf_sample_data_init(), group so as to
1042 * minimize the cachelines touched.
1043 */
1044 u64 sample_flags;
1045 u64 period;
1046
1047 /*
1048 * The other fields, optionally {set,used} by
1049 * perf_{prepare,output}_sample().
1050 */
1051 struct perf_branch_stack *br_stack;
1052 union perf_sample_weight weight;
1053 union perf_mem_data_src data_src;
1054 u64 txn;
1055 u64 addr;
1056 struct perf_raw_record *raw;
1057
1058 u64 type;
1059 u64 ip;
1060 struct {
1061 u32 pid;
1062 u32 tid;
1063 } tid_entry;
1064 u64 time;
1065 u64 id;
1066 u64 stream_id;
1067 struct {
1068 u32 cpu;
1069 u32 reserved;
1070 } cpu_entry;
1071 struct perf_callchain_entry *callchain;
1072 u64 aux_size;
1073
1074 struct perf_regs regs_user;
1075 struct perf_regs regs_intr;
1076 u64 stack_user_size;
1077
1078 u64 phys_addr;
1079 u64 cgroup;
1080 u64 data_page_size;
1081 u64 code_page_size;
1082 } ____cacheline_aligned;
1083
1084 /* default value for data source */
1085 #define PERF_MEM_NA (PERF_MEM_S(OP, NA) |\
1086 PERF_MEM_S(LVL, NA) |\
1087 PERF_MEM_S(SNOOP, NA) |\
1088 PERF_MEM_S(LOCK, NA) |\
1089 PERF_MEM_S(TLB, NA))
1090
perf_sample_data_init(struct perf_sample_data * data,u64 addr,u64 period)1091 static inline void perf_sample_data_init(struct perf_sample_data *data,
1092 u64 addr, u64 period)
1093 {
1094 /* remaining struct members initialized in perf_prepare_sample() */
1095 data->sample_flags = PERF_SAMPLE_PERIOD;
1096 data->period = period;
1097
1098 if (addr) {
1099 data->addr = addr;
1100 data->sample_flags |= PERF_SAMPLE_ADDR;
1101 }
1102 }
1103
1104 /*
1105 * Clear all bitfields in the perf_branch_entry.
1106 * The to and from fields are not cleared because they are
1107 * systematically modified by caller.
1108 */
perf_clear_branch_entry_bitfields(struct perf_branch_entry * br)1109 static inline void perf_clear_branch_entry_bitfields(struct perf_branch_entry *br)
1110 {
1111 br->mispred = 0;
1112 br->predicted = 0;
1113 br->in_tx = 0;
1114 br->abort = 0;
1115 br->cycles = 0;
1116 br->type = 0;
1117 br->spec = PERF_BR_SPEC_NA;
1118 br->reserved = 0;
1119 }
1120
1121 extern void perf_output_sample(struct perf_output_handle *handle,
1122 struct perf_event_header *header,
1123 struct perf_sample_data *data,
1124 struct perf_event *event);
1125 extern void perf_prepare_sample(struct perf_event_header *header,
1126 struct perf_sample_data *data,
1127 struct perf_event *event,
1128 struct pt_regs *regs);
1129
1130 extern int perf_event_overflow(struct perf_event *event,
1131 struct perf_sample_data *data,
1132 struct pt_regs *regs);
1133
1134 extern void perf_event_output_forward(struct perf_event *event,
1135 struct perf_sample_data *data,
1136 struct pt_regs *regs);
1137 extern void perf_event_output_backward(struct perf_event *event,
1138 struct perf_sample_data *data,
1139 struct pt_regs *regs);
1140 extern int perf_event_output(struct perf_event *event,
1141 struct perf_sample_data *data,
1142 struct pt_regs *regs);
1143
1144 static inline bool
__is_default_overflow_handler(perf_overflow_handler_t overflow_handler)1145 __is_default_overflow_handler(perf_overflow_handler_t overflow_handler)
1146 {
1147 if (likely(overflow_handler == perf_event_output_forward))
1148 return true;
1149 if (unlikely(overflow_handler == perf_event_output_backward))
1150 return true;
1151 return false;
1152 }
1153
1154 #define is_default_overflow_handler(event) \
1155 __is_default_overflow_handler((event)->overflow_handler)
1156
1157 #ifdef CONFIG_BPF_SYSCALL
uses_default_overflow_handler(struct perf_event * event)1158 static inline bool uses_default_overflow_handler(struct perf_event *event)
1159 {
1160 if (likely(is_default_overflow_handler(event)))
1161 return true;
1162
1163 return __is_default_overflow_handler(event->orig_overflow_handler);
1164 }
1165 #else
1166 #define uses_default_overflow_handler(event) \
1167 is_default_overflow_handler(event)
1168 #endif
1169
1170 extern void
1171 perf_event_header__init_id(struct perf_event_header *header,
1172 struct perf_sample_data *data,
1173 struct perf_event *event);
1174 extern void
1175 perf_event__output_id_sample(struct perf_event *event,
1176 struct perf_output_handle *handle,
1177 struct perf_sample_data *sample);
1178
1179 extern void
1180 perf_log_lost_samples(struct perf_event *event, u64 lost);
1181
event_has_any_exclude_flag(struct perf_event * event)1182 static inline bool event_has_any_exclude_flag(struct perf_event *event)
1183 {
1184 struct perf_event_attr *attr = &event->attr;
1185
1186 return attr->exclude_idle || attr->exclude_user ||
1187 attr->exclude_kernel || attr->exclude_hv ||
1188 attr->exclude_guest || attr->exclude_host;
1189 }
1190
is_sampling_event(struct perf_event * event)1191 static inline bool is_sampling_event(struct perf_event *event)
1192 {
1193 return event->attr.sample_period != 0;
1194 }
1195
1196 /*
1197 * Return 1 for a software event, 0 for a hardware event
1198 */
is_software_event(struct perf_event * event)1199 static inline int is_software_event(struct perf_event *event)
1200 {
1201 return event->event_caps & PERF_EV_CAP_SOFTWARE;
1202 }
1203
1204 /*
1205 * Return 1 for event in sw context, 0 for event in hw context
1206 */
in_software_context(struct perf_event * event)1207 static inline int in_software_context(struct perf_event *event)
1208 {
1209 return event->ctx->pmu->task_ctx_nr == perf_sw_context;
1210 }
1211
is_exclusive_pmu(struct pmu * pmu)1212 static inline int is_exclusive_pmu(struct pmu *pmu)
1213 {
1214 return pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE;
1215 }
1216
1217 extern struct static_key perf_swevent_enabled[PERF_COUNT_SW_MAX];
1218
1219 extern void ___perf_sw_event(u32, u64, struct pt_regs *, u64);
1220 extern void __perf_sw_event(u32, u64, struct pt_regs *, u64);
1221
1222 #ifndef perf_arch_fetch_caller_regs
perf_arch_fetch_caller_regs(struct pt_regs * regs,unsigned long ip)1223 static inline void perf_arch_fetch_caller_regs(struct pt_regs *regs, unsigned long ip) { }
1224 #endif
1225
1226 /*
1227 * When generating a perf sample in-line, instead of from an interrupt /
1228 * exception, we lack a pt_regs. This is typically used from software events
1229 * like: SW_CONTEXT_SWITCHES, SW_MIGRATIONS and the tie-in with tracepoints.
1230 *
1231 * We typically don't need a full set, but (for x86) do require:
1232 * - ip for PERF_SAMPLE_IP
1233 * - cs for user_mode() tests
1234 * - sp for PERF_SAMPLE_CALLCHAIN
1235 * - eflags for MISC bits and CALLCHAIN (see: perf_hw_regs())
1236 *
1237 * NOTE: assumes @regs is otherwise already 0 filled; this is important for
1238 * things like PERF_SAMPLE_REGS_INTR.
1239 */
perf_fetch_caller_regs(struct pt_regs * regs)1240 static inline void perf_fetch_caller_regs(struct pt_regs *regs)
1241 {
1242 perf_arch_fetch_caller_regs(regs, CALLER_ADDR0);
1243 }
1244
1245 static __always_inline void
perf_sw_event(u32 event_id,u64 nr,struct pt_regs * regs,u64 addr)1246 perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr)
1247 {
1248 if (static_key_false(&perf_swevent_enabled[event_id]))
1249 __perf_sw_event(event_id, nr, regs, addr);
1250 }
1251
1252 DECLARE_PER_CPU(struct pt_regs, __perf_regs[4]);
1253
1254 /*
1255 * 'Special' version for the scheduler, it hard assumes no recursion,
1256 * which is guaranteed by us not actually scheduling inside other swevents
1257 * because those disable preemption.
1258 */
__perf_sw_event_sched(u32 event_id,u64 nr,u64 addr)1259 static __always_inline void __perf_sw_event_sched(u32 event_id, u64 nr, u64 addr)
1260 {
1261 struct pt_regs *regs = this_cpu_ptr(&__perf_regs[0]);
1262
1263 perf_fetch_caller_regs(regs);
1264 ___perf_sw_event(event_id, nr, regs, addr);
1265 }
1266
1267 extern struct static_key_false perf_sched_events;
1268
__perf_sw_enabled(int swevt)1269 static __always_inline bool __perf_sw_enabled(int swevt)
1270 {
1271 return static_key_false(&perf_swevent_enabled[swevt]);
1272 }
1273
perf_event_task_migrate(struct task_struct * task)1274 static inline void perf_event_task_migrate(struct task_struct *task)
1275 {
1276 if (__perf_sw_enabled(PERF_COUNT_SW_CPU_MIGRATIONS))
1277 task->sched_migrated = 1;
1278 }
1279
perf_event_task_sched_in(struct task_struct * prev,struct task_struct * task)1280 static inline void perf_event_task_sched_in(struct task_struct *prev,
1281 struct task_struct *task)
1282 {
1283 if (static_branch_unlikely(&perf_sched_events))
1284 __perf_event_task_sched_in(prev, task);
1285
1286 if (__perf_sw_enabled(PERF_COUNT_SW_CPU_MIGRATIONS) &&
1287 task->sched_migrated) {
1288 __perf_sw_event_sched(PERF_COUNT_SW_CPU_MIGRATIONS, 1, 0);
1289 task->sched_migrated = 0;
1290 }
1291 }
1292
perf_event_task_sched_out(struct task_struct * prev,struct task_struct * next)1293 static inline void perf_event_task_sched_out(struct task_struct *prev,
1294 struct task_struct *next)
1295 {
1296 if (__perf_sw_enabled(PERF_COUNT_SW_CONTEXT_SWITCHES))
1297 __perf_sw_event_sched(PERF_COUNT_SW_CONTEXT_SWITCHES, 1, 0);
1298
1299 #ifdef CONFIG_CGROUP_PERF
1300 if (__perf_sw_enabled(PERF_COUNT_SW_CGROUP_SWITCHES) &&
1301 perf_cgroup_from_task(prev, NULL) !=
1302 perf_cgroup_from_task(next, NULL))
1303 __perf_sw_event_sched(PERF_COUNT_SW_CGROUP_SWITCHES, 1, 0);
1304 #endif
1305
1306 if (static_branch_unlikely(&perf_sched_events))
1307 __perf_event_task_sched_out(prev, next);
1308 }
1309
1310 extern void perf_event_mmap(struct vm_area_struct *vma);
1311
1312 extern void perf_event_ksymbol(u16 ksym_type, u64 addr, u32 len,
1313 bool unregister, const char *sym);
1314 extern void perf_event_bpf_event(struct bpf_prog *prog,
1315 enum perf_bpf_event_type type,
1316 u16 flags);
1317
1318 #ifdef CONFIG_GUEST_PERF_EVENTS
1319 extern struct perf_guest_info_callbacks __rcu *perf_guest_cbs;
perf_get_guest_cbs(void)1320 static inline struct perf_guest_info_callbacks *perf_get_guest_cbs(void)
1321 {
1322 /*
1323 * Callbacks are RCU-protected and must be READ_ONCE to avoid reloading
1324 * the callbacks between a !NULL check and dereferences, to ensure
1325 * pending stores/changes to the callback pointers are visible before a
1326 * non-NULL perf_guest_cbs is visible to readers, and to prevent a
1327 * module from unloading callbacks while readers are active.
1328 */
1329 return rcu_dereference(perf_guest_cbs);
1330 }
perf_guest_state(void)1331 static inline unsigned int perf_guest_state(void)
1332 {
1333 struct perf_guest_info_callbacks *guest_cbs = perf_get_guest_cbs();
1334
1335 return guest_cbs ? guest_cbs->state() : 0;
1336 }
perf_guest_get_ip(void)1337 static inline unsigned long perf_guest_get_ip(void)
1338 {
1339 struct perf_guest_info_callbacks *guest_cbs = perf_get_guest_cbs();
1340
1341 /*
1342 * Arbitrarily return '0' in the unlikely scenario that the callbacks
1343 * are unregistered between checking guest state and getting the IP.
1344 */
1345 return guest_cbs ? guest_cbs->get_ip() : 0;
1346 }
perf_guest_handle_intel_pt_intr(void)1347 static inline unsigned int perf_guest_handle_intel_pt_intr(void)
1348 {
1349 struct perf_guest_info_callbacks *guest_cbs = perf_get_guest_cbs();
1350
1351 if (guest_cbs && guest_cbs->handle_intel_pt_intr)
1352 return guest_cbs->handle_intel_pt_intr();
1353 return 0;
1354 }
1355 extern void perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs);
1356 extern void perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs);
1357 #else
perf_guest_state(void)1358 static inline unsigned int perf_guest_state(void) { return 0; }
perf_guest_get_ip(void)1359 static inline unsigned long perf_guest_get_ip(void) { return 0; }
perf_guest_handle_intel_pt_intr(void)1360 static inline unsigned int perf_guest_handle_intel_pt_intr(void) { return 0; }
1361 #endif /* CONFIG_GUEST_PERF_EVENTS */
1362
1363 extern void perf_event_exec(void);
1364 extern void perf_event_comm(struct task_struct *tsk, bool exec);
1365 extern void perf_event_namespaces(struct task_struct *tsk);
1366 extern void perf_event_fork(struct task_struct *tsk);
1367 extern void perf_event_text_poke(const void *addr,
1368 const void *old_bytes, size_t old_len,
1369 const void *new_bytes, size_t new_len);
1370
1371 /* Callchains */
1372 DECLARE_PER_CPU(struct perf_callchain_entry, perf_callchain_entry);
1373
1374 extern void perf_callchain_user(struct perf_callchain_entry_ctx *entry, struct pt_regs *regs);
1375 extern void perf_callchain_kernel(struct perf_callchain_entry_ctx *entry, struct pt_regs *regs);
1376 extern struct perf_callchain_entry *
1377 get_perf_callchain(struct pt_regs *regs, u32 init_nr, bool kernel, bool user,
1378 u32 max_stack, bool crosstask, bool add_mark);
1379 extern struct perf_callchain_entry *perf_callchain(struct perf_event *event, struct pt_regs *regs);
1380 extern int get_callchain_buffers(int max_stack);
1381 extern void put_callchain_buffers(void);
1382 extern struct perf_callchain_entry *get_callchain_entry(int *rctx);
1383 extern void put_callchain_entry(int rctx);
1384
1385 extern int sysctl_perf_event_max_stack;
1386 extern int sysctl_perf_event_max_contexts_per_stack;
1387
perf_callchain_store_context(struct perf_callchain_entry_ctx * ctx,u64 ip)1388 static inline int perf_callchain_store_context(struct perf_callchain_entry_ctx *ctx, u64 ip)
1389 {
1390 if (ctx->contexts < sysctl_perf_event_max_contexts_per_stack) {
1391 struct perf_callchain_entry *entry = ctx->entry;
1392 entry->ip[entry->nr++] = ip;
1393 ++ctx->contexts;
1394 return 0;
1395 } else {
1396 ctx->contexts_maxed = true;
1397 return -1; /* no more room, stop walking the stack */
1398 }
1399 }
1400
perf_callchain_store(struct perf_callchain_entry_ctx * ctx,u64 ip)1401 static inline int perf_callchain_store(struct perf_callchain_entry_ctx *ctx, u64 ip)
1402 {
1403 if (ctx->nr < ctx->max_stack && !ctx->contexts_maxed) {
1404 struct perf_callchain_entry *entry = ctx->entry;
1405 entry->ip[entry->nr++] = ip;
1406 ++ctx->nr;
1407 return 0;
1408 } else {
1409 return -1; /* no more room, stop walking the stack */
1410 }
1411 }
1412
1413 extern int sysctl_perf_event_paranoid;
1414 extern int sysctl_perf_event_mlock;
1415 extern int sysctl_perf_event_sample_rate;
1416 extern int sysctl_perf_cpu_time_max_percent;
1417
1418 extern void perf_sample_event_took(u64 sample_len_ns);
1419
1420 int perf_proc_update_handler(struct ctl_table *table, int write,
1421 void *buffer, size_t *lenp, loff_t *ppos);
1422 int perf_cpu_time_max_percent_handler(struct ctl_table *table, int write,
1423 void *buffer, size_t *lenp, loff_t *ppos);
1424 int perf_event_max_stack_handler(struct ctl_table *table, int write,
1425 void *buffer, size_t *lenp, loff_t *ppos);
1426
1427 /* Access to perf_event_open(2) syscall. */
1428 #define PERF_SECURITY_OPEN 0
1429
1430 /* Finer grained perf_event_open(2) access control. */
1431 #define PERF_SECURITY_CPU 1
1432 #define PERF_SECURITY_KERNEL 2
1433 #define PERF_SECURITY_TRACEPOINT 3
1434
perf_is_paranoid(void)1435 static inline int perf_is_paranoid(void)
1436 {
1437 return sysctl_perf_event_paranoid > -1;
1438 }
1439
perf_allow_kernel(struct perf_event_attr * attr)1440 static inline int perf_allow_kernel(struct perf_event_attr *attr)
1441 {
1442 if (sysctl_perf_event_paranoid > 1 && !perfmon_capable())
1443 return -EACCES;
1444
1445 return security_perf_event_open(attr, PERF_SECURITY_KERNEL);
1446 }
1447
perf_allow_cpu(struct perf_event_attr * attr)1448 static inline int perf_allow_cpu(struct perf_event_attr *attr)
1449 {
1450 if (sysctl_perf_event_paranoid > 0 && !perfmon_capable())
1451 return -EACCES;
1452
1453 return security_perf_event_open(attr, PERF_SECURITY_CPU);
1454 }
1455
perf_allow_tracepoint(struct perf_event_attr * attr)1456 static inline int perf_allow_tracepoint(struct perf_event_attr *attr)
1457 {
1458 if (sysctl_perf_event_paranoid > -1 && !perfmon_capable())
1459 return -EPERM;
1460
1461 return security_perf_event_open(attr, PERF_SECURITY_TRACEPOINT);
1462 }
1463
1464 extern void perf_event_init(void);
1465 extern void perf_tp_event(u16 event_type, u64 count, void *record,
1466 int entry_size, struct pt_regs *regs,
1467 struct hlist_head *head, int rctx,
1468 struct task_struct *task);
1469 extern void perf_bp_event(struct perf_event *event, void *data);
1470
1471 #ifndef perf_misc_flags
1472 # define perf_misc_flags(regs) \
1473 (user_mode(regs) ? PERF_RECORD_MISC_USER : PERF_RECORD_MISC_KERNEL)
1474 # define perf_instruction_pointer(regs) instruction_pointer(regs)
1475 #endif
1476 #ifndef perf_arch_bpf_user_pt_regs
1477 # define perf_arch_bpf_user_pt_regs(regs) regs
1478 #endif
1479
has_branch_stack(struct perf_event * event)1480 static inline bool has_branch_stack(struct perf_event *event)
1481 {
1482 return event->attr.sample_type & PERF_SAMPLE_BRANCH_STACK;
1483 }
1484
needs_branch_stack(struct perf_event * event)1485 static inline bool needs_branch_stack(struct perf_event *event)
1486 {
1487 return event->attr.branch_sample_type != 0;
1488 }
1489
has_aux(struct perf_event * event)1490 static inline bool has_aux(struct perf_event *event)
1491 {
1492 return event->pmu->setup_aux;
1493 }
1494
is_write_backward(struct perf_event * event)1495 static inline bool is_write_backward(struct perf_event *event)
1496 {
1497 return !!event->attr.write_backward;
1498 }
1499
has_addr_filter(struct perf_event * event)1500 static inline bool has_addr_filter(struct perf_event *event)
1501 {
1502 return event->pmu->nr_addr_filters;
1503 }
1504
1505 /*
1506 * An inherited event uses parent's filters
1507 */
1508 static inline struct perf_addr_filters_head *
perf_event_addr_filters(struct perf_event * event)1509 perf_event_addr_filters(struct perf_event *event)
1510 {
1511 struct perf_addr_filters_head *ifh = &event->addr_filters;
1512
1513 if (event->parent)
1514 ifh = &event->parent->addr_filters;
1515
1516 return ifh;
1517 }
1518
1519 extern void perf_event_addr_filters_sync(struct perf_event *event);
1520 extern void perf_report_aux_output_id(struct perf_event *event, u64 hw_id);
1521
1522 extern int perf_output_begin(struct perf_output_handle *handle,
1523 struct perf_sample_data *data,
1524 struct perf_event *event, unsigned int size);
1525 extern int perf_output_begin_forward(struct perf_output_handle *handle,
1526 struct perf_sample_data *data,
1527 struct perf_event *event,
1528 unsigned int size);
1529 extern int perf_output_begin_backward(struct perf_output_handle *handle,
1530 struct perf_sample_data *data,
1531 struct perf_event *event,
1532 unsigned int size);
1533
1534 extern void perf_output_end(struct perf_output_handle *handle);
1535 extern unsigned int perf_output_copy(struct perf_output_handle *handle,
1536 const void *buf, unsigned int len);
1537 extern unsigned int perf_output_skip(struct perf_output_handle *handle,
1538 unsigned int len);
1539 extern long perf_output_copy_aux(struct perf_output_handle *aux_handle,
1540 struct perf_output_handle *handle,
1541 unsigned long from, unsigned long to);
1542 extern int perf_swevent_get_recursion_context(void);
1543 extern void perf_swevent_put_recursion_context(int rctx);
1544 extern u64 perf_swevent_set_period(struct perf_event *event);
1545 extern void perf_event_enable(struct perf_event *event);
1546 extern void perf_event_disable(struct perf_event *event);
1547 extern void perf_event_disable_local(struct perf_event *event);
1548 extern void perf_event_disable_inatomic(struct perf_event *event);
1549 extern void perf_event_task_tick(void);
1550 extern int perf_event_account_interrupt(struct perf_event *event);
1551 extern int perf_event_period(struct perf_event *event, u64 value);
1552 extern u64 perf_event_pause(struct perf_event *event, bool reset);
1553 #else /* !CONFIG_PERF_EVENTS: */
1554 static inline void *
perf_aux_output_begin(struct perf_output_handle * handle,struct perf_event * event)1555 perf_aux_output_begin(struct perf_output_handle *handle,
1556 struct perf_event *event) { return NULL; }
1557 static inline void
perf_aux_output_end(struct perf_output_handle * handle,unsigned long size)1558 perf_aux_output_end(struct perf_output_handle *handle, unsigned long size)
1559 { }
1560 static inline int
perf_aux_output_skip(struct perf_output_handle * handle,unsigned long size)1561 perf_aux_output_skip(struct perf_output_handle *handle,
1562 unsigned long size) { return -EINVAL; }
1563 static inline void *
perf_get_aux(struct perf_output_handle * handle)1564 perf_get_aux(struct perf_output_handle *handle) { return NULL; }
1565 static inline void
perf_event_task_migrate(struct task_struct * task)1566 perf_event_task_migrate(struct task_struct *task) { }
1567 static inline void
perf_event_task_sched_in(struct task_struct * prev,struct task_struct * task)1568 perf_event_task_sched_in(struct task_struct *prev,
1569 struct task_struct *task) { }
1570 static inline void
perf_event_task_sched_out(struct task_struct * prev,struct task_struct * next)1571 perf_event_task_sched_out(struct task_struct *prev,
1572 struct task_struct *next) { }
perf_event_init_task(struct task_struct * child,u64 clone_flags)1573 static inline int perf_event_init_task(struct task_struct *child,
1574 u64 clone_flags) { return 0; }
perf_event_exit_task(struct task_struct * child)1575 static inline void perf_event_exit_task(struct task_struct *child) { }
perf_event_free_task(struct task_struct * task)1576 static inline void perf_event_free_task(struct task_struct *task) { }
perf_event_delayed_put(struct task_struct * task)1577 static inline void perf_event_delayed_put(struct task_struct *task) { }
perf_event_get(unsigned int fd)1578 static inline struct file *perf_event_get(unsigned int fd) { return ERR_PTR(-EINVAL); }
perf_get_event(struct file * file)1579 static inline const struct perf_event *perf_get_event(struct file *file)
1580 {
1581 return ERR_PTR(-EINVAL);
1582 }
perf_event_attrs(struct perf_event * event)1583 static inline const struct perf_event_attr *perf_event_attrs(struct perf_event *event)
1584 {
1585 return ERR_PTR(-EINVAL);
1586 }
perf_event_read_local(struct perf_event * event,u64 * value,u64 * enabled,u64 * running)1587 static inline int perf_event_read_local(struct perf_event *event, u64 *value,
1588 u64 *enabled, u64 *running)
1589 {
1590 return -EINVAL;
1591 }
perf_event_print_debug(void)1592 static inline void perf_event_print_debug(void) { }
perf_event_task_disable(void)1593 static inline int perf_event_task_disable(void) { return -EINVAL; }
perf_event_task_enable(void)1594 static inline int perf_event_task_enable(void) { return -EINVAL; }
perf_event_refresh(struct perf_event * event,int refresh)1595 static inline int perf_event_refresh(struct perf_event *event, int refresh)
1596 {
1597 return -EINVAL;
1598 }
1599
1600 static inline void
perf_sw_event(u32 event_id,u64 nr,struct pt_regs * regs,u64 addr)1601 perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr) { }
1602 static inline void
perf_bp_event(struct perf_event * event,void * data)1603 perf_bp_event(struct perf_event *event, void *data) { }
1604
perf_event_mmap(struct vm_area_struct * vma)1605 static inline void perf_event_mmap(struct vm_area_struct *vma) { }
1606
1607 typedef int (perf_ksymbol_get_name_f)(char *name, int name_len, void *data);
perf_event_ksymbol(u16 ksym_type,u64 addr,u32 len,bool unregister,const char * sym)1608 static inline void perf_event_ksymbol(u16 ksym_type, u64 addr, u32 len,
1609 bool unregister, const char *sym) { }
perf_event_bpf_event(struct bpf_prog * prog,enum perf_bpf_event_type type,u16 flags)1610 static inline void perf_event_bpf_event(struct bpf_prog *prog,
1611 enum perf_bpf_event_type type,
1612 u16 flags) { }
perf_event_exec(void)1613 static inline void perf_event_exec(void) { }
perf_event_comm(struct task_struct * tsk,bool exec)1614 static inline void perf_event_comm(struct task_struct *tsk, bool exec) { }
perf_event_namespaces(struct task_struct * tsk)1615 static inline void perf_event_namespaces(struct task_struct *tsk) { }
perf_event_fork(struct task_struct * tsk)1616 static inline void perf_event_fork(struct task_struct *tsk) { }
perf_event_text_poke(const void * addr,const void * old_bytes,size_t old_len,const void * new_bytes,size_t new_len)1617 static inline void perf_event_text_poke(const void *addr,
1618 const void *old_bytes,
1619 size_t old_len,
1620 const void *new_bytes,
1621 size_t new_len) { }
perf_event_init(void)1622 static inline void perf_event_init(void) { }
perf_swevent_get_recursion_context(void)1623 static inline int perf_swevent_get_recursion_context(void) { return -1; }
perf_swevent_put_recursion_context(int rctx)1624 static inline void perf_swevent_put_recursion_context(int rctx) { }
perf_swevent_set_period(struct perf_event * event)1625 static inline u64 perf_swevent_set_period(struct perf_event *event) { return 0; }
perf_event_enable(struct perf_event * event)1626 static inline void perf_event_enable(struct perf_event *event) { }
perf_event_disable(struct perf_event * event)1627 static inline void perf_event_disable(struct perf_event *event) { }
__perf_event_disable(void * info)1628 static inline int __perf_event_disable(void *info) { return -1; }
perf_event_task_tick(void)1629 static inline void perf_event_task_tick(void) { }
perf_event_release_kernel(struct perf_event * event)1630 static inline int perf_event_release_kernel(struct perf_event *event) { return 0; }
perf_event_period(struct perf_event * event,u64 value)1631 static inline int perf_event_period(struct perf_event *event, u64 value)
1632 {
1633 return -EINVAL;
1634 }
perf_event_pause(struct perf_event * event,bool reset)1635 static inline u64 perf_event_pause(struct perf_event *event, bool reset)
1636 {
1637 return 0;
1638 }
1639 #endif
1640
1641 #if defined(CONFIG_PERF_EVENTS) && defined(CONFIG_CPU_SUP_INTEL)
1642 extern void perf_restore_debug_store(void);
1643 #else
perf_restore_debug_store(void)1644 static inline void perf_restore_debug_store(void) { }
1645 #endif
1646
perf_raw_frag_last(const struct perf_raw_frag * frag)1647 static __always_inline bool perf_raw_frag_last(const struct perf_raw_frag *frag)
1648 {
1649 return frag->pad < sizeof(u64);
1650 }
1651
1652 #define perf_output_put(handle, x) perf_output_copy((handle), &(x), sizeof(x))
1653
1654 struct perf_pmu_events_attr {
1655 struct device_attribute attr;
1656 u64 id;
1657 const char *event_str;
1658 };
1659
1660 struct perf_pmu_events_ht_attr {
1661 struct device_attribute attr;
1662 u64 id;
1663 const char *event_str_ht;
1664 const char *event_str_noht;
1665 };
1666
1667 struct perf_pmu_events_hybrid_attr {
1668 struct device_attribute attr;
1669 u64 id;
1670 const char *event_str;
1671 u64 pmu_type;
1672 };
1673
1674 struct perf_pmu_format_hybrid_attr {
1675 struct device_attribute attr;
1676 u64 pmu_type;
1677 };
1678
1679 ssize_t perf_event_sysfs_show(struct device *dev, struct device_attribute *attr,
1680 char *page);
1681
1682 #define PMU_EVENT_ATTR(_name, _var, _id, _show) \
1683 static struct perf_pmu_events_attr _var = { \
1684 .attr = __ATTR(_name, 0444, _show, NULL), \
1685 .id = _id, \
1686 };
1687
1688 #define PMU_EVENT_ATTR_STRING(_name, _var, _str) \
1689 static struct perf_pmu_events_attr _var = { \
1690 .attr = __ATTR(_name, 0444, perf_event_sysfs_show, NULL), \
1691 .id = 0, \
1692 .event_str = _str, \
1693 };
1694
1695 #define PMU_EVENT_ATTR_ID(_name, _show, _id) \
1696 (&((struct perf_pmu_events_attr[]) { \
1697 { .attr = __ATTR(_name, 0444, _show, NULL), \
1698 .id = _id, } \
1699 })[0].attr.attr)
1700
1701 #define PMU_FORMAT_ATTR(_name, _format) \
1702 static ssize_t \
1703 _name##_show(struct device *dev, \
1704 struct device_attribute *attr, \
1705 char *page) \
1706 { \
1707 BUILD_BUG_ON(sizeof(_format) >= PAGE_SIZE); \
1708 return sprintf(page, _format "\n"); \
1709 } \
1710 \
1711 static struct device_attribute format_attr_##_name = __ATTR_RO(_name)
1712
1713 /* Performance counter hotplug functions */
1714 #ifdef CONFIG_PERF_EVENTS
1715 int perf_event_init_cpu(unsigned int cpu);
1716 int perf_event_exit_cpu(unsigned int cpu);
1717 #else
1718 #define perf_event_init_cpu NULL
1719 #define perf_event_exit_cpu NULL
1720 #endif
1721
1722 extern void __weak arch_perf_update_userpage(struct perf_event *event,
1723 struct perf_event_mmap_page *userpg,
1724 u64 now);
1725
1726 #ifdef CONFIG_MMU
1727 extern __weak u64 arch_perf_get_page_size(struct mm_struct *mm, unsigned long addr);
1728 #endif
1729
1730 /*
1731 * Snapshot branch stack on software events.
1732 *
1733 * Branch stack can be very useful in understanding software events. For
1734 * example, when a long function, e.g. sys_perf_event_open, returns an
1735 * errno, it is not obvious why the function failed. Branch stack could
1736 * provide very helpful information in this type of scenarios.
1737 *
1738 * On software event, it is necessary to stop the hardware branch recorder
1739 * fast. Otherwise, the hardware register/buffer will be flushed with
1740 * entries of the triggering event. Therefore, static call is used to
1741 * stop the hardware recorder.
1742 */
1743
1744 /*
1745 * cnt is the number of entries allocated for entries.
1746 * Return number of entries copied to .
1747 */
1748 typedef int (perf_snapshot_branch_stack_t)(struct perf_branch_entry *entries,
1749 unsigned int cnt);
1750 DECLARE_STATIC_CALL(perf_snapshot_branch_stack, perf_snapshot_branch_stack_t);
1751
1752 #ifndef PERF_NEEDS_LOPWR_CB
perf_lopwr_cb(bool mode)1753 static inline void perf_lopwr_cb(bool mode)
1754 {
1755 }
1756 #endif
1757
1758 #ifdef CONFIG_PERF_EVENTS
branch_sample_no_flags(const struct perf_event * event)1759 static inline bool branch_sample_no_flags(const struct perf_event *event)
1760 {
1761 return event->attr.branch_sample_type & PERF_SAMPLE_BRANCH_NO_FLAGS;
1762 }
1763
branch_sample_no_cycles(const struct perf_event * event)1764 static inline bool branch_sample_no_cycles(const struct perf_event *event)
1765 {
1766 return event->attr.branch_sample_type & PERF_SAMPLE_BRANCH_NO_CYCLES;
1767 }
1768
branch_sample_type(const struct perf_event * event)1769 static inline bool branch_sample_type(const struct perf_event *event)
1770 {
1771 return event->attr.branch_sample_type & PERF_SAMPLE_BRANCH_TYPE_SAVE;
1772 }
1773
branch_sample_hw_index(const struct perf_event * event)1774 static inline bool branch_sample_hw_index(const struct perf_event *event)
1775 {
1776 return event->attr.branch_sample_type & PERF_SAMPLE_BRANCH_HW_INDEX;
1777 }
1778
branch_sample_priv(const struct perf_event * event)1779 static inline bool branch_sample_priv(const struct perf_event *event)
1780 {
1781 return event->attr.branch_sample_type & PERF_SAMPLE_BRANCH_PRIV_SAVE;
1782 }
1783 #endif /* CONFIG_PERF_EVENTS */
1784 #endif /* _LINUX_PERF_EVENT_H */
1785