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