perf core: Pass max stack as a perf_callchain_entry context
[deliverable/linux.git] / include / linux / perf_event.h
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
19 /*
20 * Kernel-internal data types and definitions:
21 */
22
23 #ifdef CONFIG_PERF_EVENTS
24 # include <asm/perf_event.h>
25 # include <asm/local64.h>
26 #endif
27
28 struct perf_guest_info_callbacks {
29 int (*is_in_guest)(void);
30 int (*is_user_mode)(void);
31 unsigned long (*get_guest_ip)(void);
32 };
33
34 #ifdef CONFIG_HAVE_HW_BREAKPOINT
35 #include <asm/hw_breakpoint.h>
36 #endif
37
38 #include <linux/list.h>
39 #include <linux/mutex.h>
40 #include <linux/rculist.h>
41 #include <linux/rcupdate.h>
42 #include <linux/spinlock.h>
43 #include <linux/hrtimer.h>
44 #include <linux/fs.h>
45 #include <linux/pid_namespace.h>
46 #include <linux/workqueue.h>
47 #include <linux/ftrace.h>
48 #include <linux/cpu.h>
49 #include <linux/irq_work.h>
50 #include <linux/static_key.h>
51 #include <linux/jump_label_ratelimit.h>
52 #include <linux/atomic.h>
53 #include <linux/sysfs.h>
54 #include <linux/perf_regs.h>
55 #include <linux/workqueue.h>
56 #include <linux/cgroup.h>
57 #include <asm/local.h>
58
59 struct perf_callchain_entry {
60 __u64 nr;
61 __u64 ip[0]; /* /proc/sys/kernel/perf_event_max_stack */
62 };
63
64 struct perf_callchain_entry_ctx {
65 struct perf_callchain_entry *entry;
66 u32 max_stack;
67 };
68
69 struct perf_raw_record {
70 u32 size;
71 void *data;
72 };
73
74 /*
75 * branch stack layout:
76 * nr: number of taken branches stored in entries[]
77 *
78 * Note that nr can vary from sample to sample
79 * branches (to, from) are stored from most recent
80 * to least recent, i.e., entries[0] contains the most
81 * recent branch.
82 */
83 struct perf_branch_stack {
84 __u64 nr;
85 struct perf_branch_entry entries[0];
86 };
87
88 struct task_struct;
89
90 /*
91 * extra PMU register associated with an event
92 */
93 struct hw_perf_event_extra {
94 u64 config; /* register value */
95 unsigned int reg; /* register address or index */
96 int alloc; /* extra register already allocated */
97 int idx; /* index in shared_regs->regs[] */
98 };
99
100 /**
101 * struct hw_perf_event - performance event hardware details:
102 */
103 struct hw_perf_event {
104 #ifdef CONFIG_PERF_EVENTS
105 union {
106 struct { /* hardware */
107 u64 config;
108 u64 last_tag;
109 unsigned long config_base;
110 unsigned long event_base;
111 int event_base_rdpmc;
112 int idx;
113 int last_cpu;
114 int flags;
115
116 struct hw_perf_event_extra extra_reg;
117 struct hw_perf_event_extra branch_reg;
118 };
119 struct { /* software */
120 struct hrtimer hrtimer;
121 };
122 struct { /* tracepoint */
123 /* for tp_event->class */
124 struct list_head tp_list;
125 };
126 struct { /* intel_cqm */
127 int cqm_state;
128 u32 cqm_rmid;
129 int is_group_event;
130 struct list_head cqm_events_entry;
131 struct list_head cqm_groups_entry;
132 struct list_head cqm_group_entry;
133 };
134 struct { /* itrace */
135 int itrace_started;
136 };
137 struct { /* amd_power */
138 u64 pwr_acc;
139 u64 ptsc;
140 };
141 #ifdef CONFIG_HAVE_HW_BREAKPOINT
142 struct { /* breakpoint */
143 /*
144 * Crufty hack to avoid the chicken and egg
145 * problem hw_breakpoint has with context
146 * creation and event initalization.
147 */
148 struct arch_hw_breakpoint info;
149 struct list_head bp_list;
150 };
151 #endif
152 };
153 /*
154 * If the event is a per task event, this will point to the task in
155 * question. See the comment in perf_event_alloc().
156 */
157 struct task_struct *target;
158
159 /*
160 * PMU would store hardware filter configuration
161 * here.
162 */
163 void *addr_filters;
164
165 /* Last sync'ed generation of filters */
166 unsigned long addr_filters_gen;
167
168 /*
169 * hw_perf_event::state flags; used to track the PERF_EF_* state.
170 */
171 #define PERF_HES_STOPPED 0x01 /* the counter is stopped */
172 #define PERF_HES_UPTODATE 0x02 /* event->count up-to-date */
173 #define PERF_HES_ARCH 0x04
174
175 int state;
176
177 /*
178 * The last observed hardware counter value, updated with a
179 * local64_cmpxchg() such that pmu::read() can be called nested.
180 */
181 local64_t prev_count;
182
183 /*
184 * The period to start the next sample with.
185 */
186 u64 sample_period;
187
188 /*
189 * The period we started this sample with.
190 */
191 u64 last_period;
192
193 /*
194 * However much is left of the current period; note that this is
195 * a full 64bit value and allows for generation of periods longer
196 * than hardware might allow.
197 */
198 local64_t period_left;
199
200 /*
201 * State for throttling the event, see __perf_event_overflow() and
202 * perf_adjust_freq_unthr_context().
203 */
204 u64 interrupts_seq;
205 u64 interrupts;
206
207 /*
208 * State for freq target events, see __perf_event_overflow() and
209 * perf_adjust_freq_unthr_context().
210 */
211 u64 freq_time_stamp;
212 u64 freq_count_stamp;
213 #endif
214 };
215
216 struct perf_event;
217
218 /*
219 * Common implementation detail of pmu::{start,commit,cancel}_txn
220 */
221 #define PERF_PMU_TXN_ADD 0x1 /* txn to add/schedule event on PMU */
222 #define PERF_PMU_TXN_READ 0x2 /* txn to read event group from PMU */
223
224 /**
225 * pmu::capabilities flags
226 */
227 #define PERF_PMU_CAP_NO_INTERRUPT 0x01
228 #define PERF_PMU_CAP_NO_NMI 0x02
229 #define PERF_PMU_CAP_AUX_NO_SG 0x04
230 #define PERF_PMU_CAP_AUX_SW_DOUBLEBUF 0x08
231 #define PERF_PMU_CAP_EXCLUSIVE 0x10
232 #define PERF_PMU_CAP_ITRACE 0x20
233 #define PERF_PMU_CAP_HETEROGENEOUS_CPUS 0x40
234
235 /**
236 * struct pmu - generic performance monitoring unit
237 */
238 struct pmu {
239 struct list_head entry;
240
241 struct module *module;
242 struct device *dev;
243 const struct attribute_group **attr_groups;
244 const char *name;
245 int type;
246
247 /*
248 * various common per-pmu feature flags
249 */
250 int capabilities;
251
252 int * __percpu pmu_disable_count;
253 struct perf_cpu_context * __percpu pmu_cpu_context;
254 atomic_t exclusive_cnt; /* < 0: cpu; > 0: tsk */
255 int task_ctx_nr;
256 int hrtimer_interval_ms;
257
258 /* number of address filters this PMU can do */
259 unsigned int nr_addr_filters;
260
261 /*
262 * Fully disable/enable this PMU, can be used to protect from the PMI
263 * as well as for lazy/batch writing of the MSRs.
264 */
265 void (*pmu_enable) (struct pmu *pmu); /* optional */
266 void (*pmu_disable) (struct pmu *pmu); /* optional */
267
268 /*
269 * Try and initialize the event for this PMU.
270 *
271 * Returns:
272 * -ENOENT -- @event is not for this PMU
273 *
274 * -ENODEV -- @event is for this PMU but PMU not present
275 * -EBUSY -- @event is for this PMU but PMU temporarily unavailable
276 * -EINVAL -- @event is for this PMU but @event is not valid
277 * -EOPNOTSUPP -- @event is for this PMU, @event is valid, but not supported
278 * -EACCESS -- @event is for this PMU, @event is valid, but no privilidges
279 *
280 * 0 -- @event is for this PMU and valid
281 *
282 * Other error return values are allowed.
283 */
284 int (*event_init) (struct perf_event *event);
285
286 /*
287 * Notification that the event was mapped or unmapped. Called
288 * in the context of the mapping task.
289 */
290 void (*event_mapped) (struct perf_event *event); /*optional*/
291 void (*event_unmapped) (struct perf_event *event); /*optional*/
292
293 /*
294 * Flags for ->add()/->del()/ ->start()/->stop(). There are
295 * matching hw_perf_event::state flags.
296 */
297 #define PERF_EF_START 0x01 /* start the counter when adding */
298 #define PERF_EF_RELOAD 0x02 /* reload the counter when starting */
299 #define PERF_EF_UPDATE 0x04 /* update the counter when stopping */
300
301 /*
302 * Adds/Removes a counter to/from the PMU, can be done inside a
303 * transaction, see the ->*_txn() methods.
304 *
305 * The add/del callbacks will reserve all hardware resources required
306 * to service the event, this includes any counter constraint
307 * scheduling etc.
308 *
309 * Called with IRQs disabled and the PMU disabled on the CPU the event
310 * is on.
311 *
312 * ->add() called without PERF_EF_START should result in the same state
313 * as ->add() followed by ->stop().
314 *
315 * ->del() must always PERF_EF_UPDATE stop an event. If it calls
316 * ->stop() that must deal with already being stopped without
317 * PERF_EF_UPDATE.
318 */
319 int (*add) (struct perf_event *event, int flags);
320 void (*del) (struct perf_event *event, int flags);
321
322 /*
323 * Starts/Stops a counter present on the PMU.
324 *
325 * The PMI handler should stop the counter when perf_event_overflow()
326 * returns !0. ->start() will be used to continue.
327 *
328 * Also used to change the sample period.
329 *
330 * Called with IRQs disabled and the PMU disabled on the CPU the event
331 * is on -- will be called from NMI context with the PMU generates
332 * NMIs.
333 *
334 * ->stop() with PERF_EF_UPDATE will read the counter and update
335 * period/count values like ->read() would.
336 *
337 * ->start() with PERF_EF_RELOAD will reprogram the the counter
338 * value, must be preceded by a ->stop() with PERF_EF_UPDATE.
339 */
340 void (*start) (struct perf_event *event, int flags);
341 void (*stop) (struct perf_event *event, int flags);
342
343 /*
344 * Updates the counter value of the event.
345 *
346 * For sampling capable PMUs this will also update the software period
347 * hw_perf_event::period_left field.
348 */
349 void (*read) (struct perf_event *event);
350
351 /*
352 * Group events scheduling is treated as a transaction, add
353 * group events as a whole and perform one schedulability test.
354 * If the test fails, roll back the whole group
355 *
356 * Start the transaction, after this ->add() doesn't need to
357 * do schedulability tests.
358 *
359 * Optional.
360 */
361 void (*start_txn) (struct pmu *pmu, unsigned int txn_flags);
362 /*
363 * If ->start_txn() disabled the ->add() schedulability test
364 * then ->commit_txn() is required to perform one. On success
365 * the transaction is closed. On error the transaction is kept
366 * open until ->cancel_txn() is called.
367 *
368 * Optional.
369 */
370 int (*commit_txn) (struct pmu *pmu);
371 /*
372 * Will cancel the transaction, assumes ->del() is called
373 * for each successful ->add() during the transaction.
374 *
375 * Optional.
376 */
377 void (*cancel_txn) (struct pmu *pmu);
378
379 /*
380 * Will return the value for perf_event_mmap_page::index for this event,
381 * if no implementation is provided it will default to: event->hw.idx + 1.
382 */
383 int (*event_idx) (struct perf_event *event); /*optional */
384
385 /*
386 * context-switches callback
387 */
388 void (*sched_task) (struct perf_event_context *ctx,
389 bool sched_in);
390 /*
391 * PMU specific data size
392 */
393 size_t task_ctx_size;
394
395
396 /*
397 * Return the count value for a counter.
398 */
399 u64 (*count) (struct perf_event *event); /*optional*/
400
401 /*
402 * Set up pmu-private data structures for an AUX area
403 */
404 void *(*setup_aux) (int cpu, void **pages,
405 int nr_pages, bool overwrite);
406 /* optional */
407
408 /*
409 * Free pmu-private AUX data structures
410 */
411 void (*free_aux) (void *aux); /* optional */
412
413 /*
414 * Validate address range filters: make sure the HW supports the
415 * requested configuration and number of filters; return 0 if the
416 * supplied filters are valid, -errno otherwise.
417 *
418 * Runs in the context of the ioctl()ing process and is not serialized
419 * with the rest of the PMU callbacks.
420 */
421 int (*addr_filters_validate) (struct list_head *filters);
422 /* optional */
423
424 /*
425 * Synchronize address range filter configuration:
426 * translate hw-agnostic filters into hardware configuration in
427 * event::hw::addr_filters.
428 *
429 * Runs as a part of filter sync sequence that is done in ->start()
430 * callback by calling perf_event_addr_filters_sync().
431 *
432 * May (and should) traverse event::addr_filters::list, for which its
433 * caller provides necessary serialization.
434 */
435 void (*addr_filters_sync) (struct perf_event *event);
436 /* optional */
437
438 /*
439 * Filter events for PMU-specific reasons.
440 */
441 int (*filter_match) (struct perf_event *event); /* optional */
442 };
443
444 /**
445 * struct perf_addr_filter - address range filter definition
446 * @entry: event's filter list linkage
447 * @inode: object file's inode for file-based filters
448 * @offset: filter range offset
449 * @size: filter range size
450 * @range: 1: range, 0: address
451 * @filter: 1: filter/start, 0: stop
452 *
453 * This is a hardware-agnostic filter configuration as specified by the user.
454 */
455 struct perf_addr_filter {
456 struct list_head entry;
457 struct inode *inode;
458 unsigned long offset;
459 unsigned long size;
460 unsigned int range : 1,
461 filter : 1;
462 };
463
464 /**
465 * struct perf_addr_filters_head - container for address range filters
466 * @list: list of filters for this event
467 * @lock: spinlock that serializes accesses to the @list and event's
468 * (and its children's) filter generations.
469 *
470 * A child event will use parent's @list (and therefore @lock), so they are
471 * bundled together; see perf_event_addr_filters().
472 */
473 struct perf_addr_filters_head {
474 struct list_head list;
475 raw_spinlock_t lock;
476 };
477
478 /**
479 * enum perf_event_active_state - the states of a event
480 */
481 enum perf_event_active_state {
482 PERF_EVENT_STATE_DEAD = -4,
483 PERF_EVENT_STATE_EXIT = -3,
484 PERF_EVENT_STATE_ERROR = -2,
485 PERF_EVENT_STATE_OFF = -1,
486 PERF_EVENT_STATE_INACTIVE = 0,
487 PERF_EVENT_STATE_ACTIVE = 1,
488 };
489
490 struct file;
491 struct perf_sample_data;
492
493 typedef void (*perf_overflow_handler_t)(struct perf_event *,
494 struct perf_sample_data *,
495 struct pt_regs *regs);
496
497 enum perf_group_flag {
498 PERF_GROUP_SOFTWARE = 0x1,
499 };
500
501 #define SWEVENT_HLIST_BITS 8
502 #define SWEVENT_HLIST_SIZE (1 << SWEVENT_HLIST_BITS)
503
504 struct swevent_hlist {
505 struct hlist_head heads[SWEVENT_HLIST_SIZE];
506 struct rcu_head rcu_head;
507 };
508
509 #define PERF_ATTACH_CONTEXT 0x01
510 #define PERF_ATTACH_GROUP 0x02
511 #define PERF_ATTACH_TASK 0x04
512 #define PERF_ATTACH_TASK_DATA 0x08
513
514 struct perf_cgroup;
515 struct ring_buffer;
516
517 /**
518 * struct perf_event - performance event kernel representation:
519 */
520 struct perf_event {
521 #ifdef CONFIG_PERF_EVENTS
522 /*
523 * entry onto perf_event_context::event_list;
524 * modifications require ctx->lock
525 * RCU safe iterations.
526 */
527 struct list_head event_entry;
528
529 /*
530 * XXX: group_entry and sibling_list should be mutually exclusive;
531 * either you're a sibling on a group, or you're the group leader.
532 * Rework the code to always use the same list element.
533 *
534 * Locked for modification by both ctx->mutex and ctx->lock; holding
535 * either sufficies for read.
536 */
537 struct list_head group_entry;
538 struct list_head sibling_list;
539
540 /*
541 * We need storage to track the entries in perf_pmu_migrate_context; we
542 * cannot use the event_entry because of RCU and we want to keep the
543 * group in tact which avoids us using the other two entries.
544 */
545 struct list_head migrate_entry;
546
547 struct hlist_node hlist_entry;
548 struct list_head active_entry;
549 int nr_siblings;
550 int group_flags;
551 struct perf_event *group_leader;
552 struct pmu *pmu;
553 void *pmu_private;
554
555 enum perf_event_active_state state;
556 unsigned int attach_state;
557 local64_t count;
558 atomic64_t child_count;
559
560 /*
561 * These are the total time in nanoseconds that the event
562 * has been enabled (i.e. eligible to run, and the task has
563 * been scheduled in, if this is a per-task event)
564 * and running (scheduled onto the CPU), respectively.
565 *
566 * They are computed from tstamp_enabled, tstamp_running and
567 * tstamp_stopped when the event is in INACTIVE or ACTIVE state.
568 */
569 u64 total_time_enabled;
570 u64 total_time_running;
571
572 /*
573 * These are timestamps used for computing total_time_enabled
574 * and total_time_running when the event is in INACTIVE or
575 * ACTIVE state, measured in nanoseconds from an arbitrary point
576 * in time.
577 * tstamp_enabled: the notional time when the event was enabled
578 * tstamp_running: the notional time when the event was scheduled on
579 * tstamp_stopped: in INACTIVE state, the notional time when the
580 * event was scheduled off.
581 */
582 u64 tstamp_enabled;
583 u64 tstamp_running;
584 u64 tstamp_stopped;
585
586 /*
587 * timestamp shadows the actual context timing but it can
588 * be safely used in NMI interrupt context. It reflects the
589 * context time as it was when the event was last scheduled in.
590 *
591 * ctx_time already accounts for ctx->timestamp. Therefore to
592 * compute ctx_time for a sample, simply add perf_clock().
593 */
594 u64 shadow_ctx_time;
595
596 struct perf_event_attr attr;
597 u16 header_size;
598 u16 id_header_size;
599 u16 read_size;
600 struct hw_perf_event hw;
601
602 struct perf_event_context *ctx;
603 atomic_long_t refcount;
604
605 /*
606 * These accumulate total time (in nanoseconds) that children
607 * events have been enabled and running, respectively.
608 */
609 atomic64_t child_total_time_enabled;
610 atomic64_t child_total_time_running;
611
612 /*
613 * Protect attach/detach and child_list:
614 */
615 struct mutex child_mutex;
616 struct list_head child_list;
617 struct perf_event *parent;
618
619 int oncpu;
620 int cpu;
621
622 struct list_head owner_entry;
623 struct task_struct *owner;
624
625 /* mmap bits */
626 struct mutex mmap_mutex;
627 atomic_t mmap_count;
628
629 struct ring_buffer *rb;
630 struct list_head rb_entry;
631 unsigned long rcu_batches;
632 int rcu_pending;
633
634 /* poll related */
635 wait_queue_head_t waitq;
636 struct fasync_struct *fasync;
637
638 /* delayed work for NMIs and such */
639 int pending_wakeup;
640 int pending_kill;
641 int pending_disable;
642 struct irq_work pending;
643
644 atomic_t event_limit;
645
646 /* address range filters */
647 struct perf_addr_filters_head addr_filters;
648 /* vma address array for file-based filders */
649 unsigned long *addr_filters_offs;
650 unsigned long addr_filters_gen;
651
652 void (*destroy)(struct perf_event *);
653 struct rcu_head rcu_head;
654
655 struct pid_namespace *ns;
656 u64 id;
657
658 u64 (*clock)(void);
659 perf_overflow_handler_t overflow_handler;
660 void *overflow_handler_context;
661
662 #ifdef CONFIG_EVENT_TRACING
663 struct trace_event_call *tp_event;
664 struct event_filter *filter;
665 #ifdef CONFIG_FUNCTION_TRACER
666 struct ftrace_ops ftrace_ops;
667 #endif
668 #endif
669
670 #ifdef CONFIG_CGROUP_PERF
671 struct perf_cgroup *cgrp; /* cgroup event is attach to */
672 int cgrp_defer_enabled;
673 #endif
674
675 #endif /* CONFIG_PERF_EVENTS */
676 };
677
678 /**
679 * struct perf_event_context - event context structure
680 *
681 * Used as a container for task events and CPU events as well:
682 */
683 struct perf_event_context {
684 struct pmu *pmu;
685 /*
686 * Protect the states of the events in the list,
687 * nr_active, and the list:
688 */
689 raw_spinlock_t lock;
690 /*
691 * Protect the list of events. Locking either mutex or lock
692 * is sufficient to ensure the list doesn't change; to change
693 * the list you need to lock both the mutex and the spinlock.
694 */
695 struct mutex mutex;
696
697 struct list_head active_ctx_list;
698 struct list_head pinned_groups;
699 struct list_head flexible_groups;
700 struct list_head event_list;
701 int nr_events;
702 int nr_active;
703 int is_active;
704 int nr_stat;
705 int nr_freq;
706 int rotate_disable;
707 atomic_t refcount;
708 struct task_struct *task;
709
710 /*
711 * Context clock, runs when context enabled.
712 */
713 u64 time;
714 u64 timestamp;
715
716 /*
717 * These fields let us detect when two contexts have both
718 * been cloned (inherited) from a common ancestor.
719 */
720 struct perf_event_context *parent_ctx;
721 u64 parent_gen;
722 u64 generation;
723 int pin_count;
724 int nr_cgroups; /* cgroup evts */
725 void *task_ctx_data; /* pmu specific data */
726 struct rcu_head rcu_head;
727 };
728
729 /*
730 * Number of contexts where an event can trigger:
731 * task, softirq, hardirq, nmi.
732 */
733 #define PERF_NR_CONTEXTS 4
734
735 /**
736 * struct perf_event_cpu_context - per cpu event context structure
737 */
738 struct perf_cpu_context {
739 struct perf_event_context ctx;
740 struct perf_event_context *task_ctx;
741 int active_oncpu;
742 int exclusive;
743
744 raw_spinlock_t hrtimer_lock;
745 struct hrtimer hrtimer;
746 ktime_t hrtimer_interval;
747 unsigned int hrtimer_active;
748
749 struct pmu *unique_pmu;
750 struct perf_cgroup *cgrp;
751 };
752
753 struct perf_output_handle {
754 struct perf_event *event;
755 struct ring_buffer *rb;
756 unsigned long wakeup;
757 unsigned long size;
758 union {
759 void *addr;
760 unsigned long head;
761 };
762 int page;
763 };
764
765 #ifdef CONFIG_CGROUP_PERF
766
767 /*
768 * perf_cgroup_info keeps track of time_enabled for a cgroup.
769 * This is a per-cpu dynamically allocated data structure.
770 */
771 struct perf_cgroup_info {
772 u64 time;
773 u64 timestamp;
774 };
775
776 struct perf_cgroup {
777 struct cgroup_subsys_state css;
778 struct perf_cgroup_info __percpu *info;
779 };
780
781 /*
782 * Must ensure cgroup is pinned (css_get) before calling
783 * this function. In other words, we cannot call this function
784 * if there is no cgroup event for the current CPU context.
785 */
786 static inline struct perf_cgroup *
787 perf_cgroup_from_task(struct task_struct *task, struct perf_event_context *ctx)
788 {
789 return container_of(task_css_check(task, perf_event_cgrp_id,
790 ctx ? lockdep_is_held(&ctx->lock)
791 : true),
792 struct perf_cgroup, css);
793 }
794 #endif /* CONFIG_CGROUP_PERF */
795
796 #ifdef CONFIG_PERF_EVENTS
797
798 extern void *perf_aux_output_begin(struct perf_output_handle *handle,
799 struct perf_event *event);
800 extern void perf_aux_output_end(struct perf_output_handle *handle,
801 unsigned long size, bool truncated);
802 extern int perf_aux_output_skip(struct perf_output_handle *handle,
803 unsigned long size);
804 extern void *perf_get_aux(struct perf_output_handle *handle);
805
806 extern int perf_pmu_register(struct pmu *pmu, const char *name, int type);
807 extern void perf_pmu_unregister(struct pmu *pmu);
808
809 extern int perf_num_counters(void);
810 extern const char *perf_pmu_name(void);
811 extern void __perf_event_task_sched_in(struct task_struct *prev,
812 struct task_struct *task);
813 extern void __perf_event_task_sched_out(struct task_struct *prev,
814 struct task_struct *next);
815 extern int perf_event_init_task(struct task_struct *child);
816 extern void perf_event_exit_task(struct task_struct *child);
817 extern void perf_event_free_task(struct task_struct *task);
818 extern void perf_event_delayed_put(struct task_struct *task);
819 extern struct file *perf_event_get(unsigned int fd);
820 extern const struct perf_event_attr *perf_event_attrs(struct perf_event *event);
821 extern void perf_event_print_debug(void);
822 extern void perf_pmu_disable(struct pmu *pmu);
823 extern void perf_pmu_enable(struct pmu *pmu);
824 extern void perf_sched_cb_dec(struct pmu *pmu);
825 extern void perf_sched_cb_inc(struct pmu *pmu);
826 extern int perf_event_task_disable(void);
827 extern int perf_event_task_enable(void);
828 extern int perf_event_refresh(struct perf_event *event, int refresh);
829 extern void perf_event_update_userpage(struct perf_event *event);
830 extern int perf_event_release_kernel(struct perf_event *event);
831 extern struct perf_event *
832 perf_event_create_kernel_counter(struct perf_event_attr *attr,
833 int cpu,
834 struct task_struct *task,
835 perf_overflow_handler_t callback,
836 void *context);
837 extern void perf_pmu_migrate_context(struct pmu *pmu,
838 int src_cpu, int dst_cpu);
839 extern u64 perf_event_read_local(struct perf_event *event);
840 extern u64 perf_event_read_value(struct perf_event *event,
841 u64 *enabled, u64 *running);
842
843
844 struct perf_sample_data {
845 /*
846 * Fields set by perf_sample_data_init(), group so as to
847 * minimize the cachelines touched.
848 */
849 u64 addr;
850 struct perf_raw_record *raw;
851 struct perf_branch_stack *br_stack;
852 u64 period;
853 u64 weight;
854 u64 txn;
855 union perf_mem_data_src data_src;
856
857 /*
858 * The other fields, optionally {set,used} by
859 * perf_{prepare,output}_sample().
860 */
861 u64 type;
862 u64 ip;
863 struct {
864 u32 pid;
865 u32 tid;
866 } tid_entry;
867 u64 time;
868 u64 id;
869 u64 stream_id;
870 struct {
871 u32 cpu;
872 u32 reserved;
873 } cpu_entry;
874 struct perf_callchain_entry *callchain;
875
876 /*
877 * regs_user may point to task_pt_regs or to regs_user_copy, depending
878 * on arch details.
879 */
880 struct perf_regs regs_user;
881 struct pt_regs regs_user_copy;
882
883 struct perf_regs regs_intr;
884 u64 stack_user_size;
885 } ____cacheline_aligned;
886
887 /* default value for data source */
888 #define PERF_MEM_NA (PERF_MEM_S(OP, NA) |\
889 PERF_MEM_S(LVL, NA) |\
890 PERF_MEM_S(SNOOP, NA) |\
891 PERF_MEM_S(LOCK, NA) |\
892 PERF_MEM_S(TLB, NA))
893
894 static inline void perf_sample_data_init(struct perf_sample_data *data,
895 u64 addr, u64 period)
896 {
897 /* remaining struct members initialized in perf_prepare_sample() */
898 data->addr = addr;
899 data->raw = NULL;
900 data->br_stack = NULL;
901 data->period = period;
902 data->weight = 0;
903 data->data_src.val = PERF_MEM_NA;
904 data->txn = 0;
905 }
906
907 extern void perf_output_sample(struct perf_output_handle *handle,
908 struct perf_event_header *header,
909 struct perf_sample_data *data,
910 struct perf_event *event);
911 extern void perf_prepare_sample(struct perf_event_header *header,
912 struct perf_sample_data *data,
913 struct perf_event *event,
914 struct pt_regs *regs);
915
916 extern int perf_event_overflow(struct perf_event *event,
917 struct perf_sample_data *data,
918 struct pt_regs *regs);
919
920 extern void perf_event_output_forward(struct perf_event *event,
921 struct perf_sample_data *data,
922 struct pt_regs *regs);
923 extern void perf_event_output_backward(struct perf_event *event,
924 struct perf_sample_data *data,
925 struct pt_regs *regs);
926 extern void perf_event_output(struct perf_event *event,
927 struct perf_sample_data *data,
928 struct pt_regs *regs);
929
930 static inline bool
931 is_default_overflow_handler(struct perf_event *event)
932 {
933 if (likely(event->overflow_handler == perf_event_output_forward))
934 return true;
935 if (unlikely(event->overflow_handler == perf_event_output_backward))
936 return true;
937 return false;
938 }
939
940 extern void
941 perf_event_header__init_id(struct perf_event_header *header,
942 struct perf_sample_data *data,
943 struct perf_event *event);
944 extern void
945 perf_event__output_id_sample(struct perf_event *event,
946 struct perf_output_handle *handle,
947 struct perf_sample_data *sample);
948
949 extern void
950 perf_log_lost_samples(struct perf_event *event, u64 lost);
951
952 static inline bool is_sampling_event(struct perf_event *event)
953 {
954 return event->attr.sample_period != 0;
955 }
956
957 /*
958 * Return 1 for a software event, 0 for a hardware event
959 */
960 static inline int is_software_event(struct perf_event *event)
961 {
962 return event->pmu->task_ctx_nr == perf_sw_context;
963 }
964
965 extern struct static_key perf_swevent_enabled[PERF_COUNT_SW_MAX];
966
967 extern void ___perf_sw_event(u32, u64, struct pt_regs *, u64);
968 extern void __perf_sw_event(u32, u64, struct pt_regs *, u64);
969
970 #ifndef perf_arch_fetch_caller_regs
971 static inline void perf_arch_fetch_caller_regs(struct pt_regs *regs, unsigned long ip) { }
972 #endif
973
974 /*
975 * Take a snapshot of the regs. Skip ip and frame pointer to
976 * the nth caller. We only need a few of the regs:
977 * - ip for PERF_SAMPLE_IP
978 * - cs for user_mode() tests
979 * - bp for callchains
980 * - eflags, for future purposes, just in case
981 */
982 static inline void perf_fetch_caller_regs(struct pt_regs *regs)
983 {
984 memset(regs, 0, sizeof(*regs));
985
986 perf_arch_fetch_caller_regs(regs, CALLER_ADDR0);
987 }
988
989 static __always_inline void
990 perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr)
991 {
992 if (static_key_false(&perf_swevent_enabled[event_id]))
993 __perf_sw_event(event_id, nr, regs, addr);
994 }
995
996 DECLARE_PER_CPU(struct pt_regs, __perf_regs[4]);
997
998 /*
999 * 'Special' version for the scheduler, it hard assumes no recursion,
1000 * which is guaranteed by us not actually scheduling inside other swevents
1001 * because those disable preemption.
1002 */
1003 static __always_inline void
1004 perf_sw_event_sched(u32 event_id, u64 nr, u64 addr)
1005 {
1006 if (static_key_false(&perf_swevent_enabled[event_id])) {
1007 struct pt_regs *regs = this_cpu_ptr(&__perf_regs[0]);
1008
1009 perf_fetch_caller_regs(regs);
1010 ___perf_sw_event(event_id, nr, regs, addr);
1011 }
1012 }
1013
1014 extern struct static_key_false perf_sched_events;
1015
1016 static __always_inline bool
1017 perf_sw_migrate_enabled(void)
1018 {
1019 if (static_key_false(&perf_swevent_enabled[PERF_COUNT_SW_CPU_MIGRATIONS]))
1020 return true;
1021 return false;
1022 }
1023
1024 static inline void perf_event_task_migrate(struct task_struct *task)
1025 {
1026 if (perf_sw_migrate_enabled())
1027 task->sched_migrated = 1;
1028 }
1029
1030 static inline void perf_event_task_sched_in(struct task_struct *prev,
1031 struct task_struct *task)
1032 {
1033 if (static_branch_unlikely(&perf_sched_events))
1034 __perf_event_task_sched_in(prev, task);
1035
1036 if (perf_sw_migrate_enabled() && task->sched_migrated) {
1037 struct pt_regs *regs = this_cpu_ptr(&__perf_regs[0]);
1038
1039 perf_fetch_caller_regs(regs);
1040 ___perf_sw_event(PERF_COUNT_SW_CPU_MIGRATIONS, 1, regs, 0);
1041 task->sched_migrated = 0;
1042 }
1043 }
1044
1045 static inline void perf_event_task_sched_out(struct task_struct *prev,
1046 struct task_struct *next)
1047 {
1048 perf_sw_event_sched(PERF_COUNT_SW_CONTEXT_SWITCHES, 1, 0);
1049
1050 if (static_branch_unlikely(&perf_sched_events))
1051 __perf_event_task_sched_out(prev, next);
1052 }
1053
1054 static inline u64 __perf_event_count(struct perf_event *event)
1055 {
1056 return local64_read(&event->count) + atomic64_read(&event->child_count);
1057 }
1058
1059 extern void perf_event_mmap(struct vm_area_struct *vma);
1060 extern struct perf_guest_info_callbacks *perf_guest_cbs;
1061 extern int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *callbacks);
1062 extern int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *callbacks);
1063
1064 extern void perf_event_exec(void);
1065 extern void perf_event_comm(struct task_struct *tsk, bool exec);
1066 extern void perf_event_fork(struct task_struct *tsk);
1067
1068 /* Callchains */
1069 DECLARE_PER_CPU(struct perf_callchain_entry, perf_callchain_entry);
1070
1071 extern void perf_callchain_user(struct perf_callchain_entry_ctx *entry, struct pt_regs *regs);
1072 extern void perf_callchain_kernel(struct perf_callchain_entry_ctx *entry, struct pt_regs *regs);
1073 extern struct perf_callchain_entry *
1074 get_perf_callchain(struct pt_regs *regs, u32 init_nr, bool kernel, bool user,
1075 u32 max_stack, bool crosstask, bool add_mark);
1076 extern int get_callchain_buffers(void);
1077 extern void put_callchain_buffers(void);
1078
1079 extern int sysctl_perf_event_max_stack;
1080
1081 static inline int perf_callchain_store(struct perf_callchain_entry_ctx *ctx, u64 ip)
1082 {
1083 struct perf_callchain_entry *entry = ctx->entry;
1084 if (entry->nr < ctx->max_stack) {
1085 entry->ip[entry->nr++] = ip;
1086 return 0;
1087 } else {
1088 return -1; /* no more room, stop walking the stack */
1089 }
1090 }
1091
1092 extern int sysctl_perf_event_paranoid;
1093 extern int sysctl_perf_event_mlock;
1094 extern int sysctl_perf_event_sample_rate;
1095 extern int sysctl_perf_cpu_time_max_percent;
1096
1097 extern void perf_sample_event_took(u64 sample_len_ns);
1098
1099 extern int perf_proc_update_handler(struct ctl_table *table, int write,
1100 void __user *buffer, size_t *lenp,
1101 loff_t *ppos);
1102 extern int perf_cpu_time_max_percent_handler(struct ctl_table *table, int write,
1103 void __user *buffer, size_t *lenp,
1104 loff_t *ppos);
1105
1106 int perf_event_max_stack_handler(struct ctl_table *table, int write,
1107 void __user *buffer, size_t *lenp, loff_t *ppos);
1108
1109 static inline bool perf_paranoid_tracepoint_raw(void)
1110 {
1111 return sysctl_perf_event_paranoid > -1;
1112 }
1113
1114 static inline bool perf_paranoid_cpu(void)
1115 {
1116 return sysctl_perf_event_paranoid > 0;
1117 }
1118
1119 static inline bool perf_paranoid_kernel(void)
1120 {
1121 return sysctl_perf_event_paranoid > 1;
1122 }
1123
1124 extern void perf_event_init(void);
1125 extern void perf_tp_event(u64 addr, u64 count, void *record,
1126 int entry_size, struct pt_regs *regs,
1127 struct hlist_head *head, int rctx,
1128 struct task_struct *task);
1129 extern void perf_bp_event(struct perf_event *event, void *data);
1130
1131 #ifndef perf_misc_flags
1132 # define perf_misc_flags(regs) \
1133 (user_mode(regs) ? PERF_RECORD_MISC_USER : PERF_RECORD_MISC_KERNEL)
1134 # define perf_instruction_pointer(regs) instruction_pointer(regs)
1135 #endif
1136
1137 static inline bool has_branch_stack(struct perf_event *event)
1138 {
1139 return event->attr.sample_type & PERF_SAMPLE_BRANCH_STACK;
1140 }
1141
1142 static inline bool needs_branch_stack(struct perf_event *event)
1143 {
1144 return event->attr.branch_sample_type != 0;
1145 }
1146
1147 static inline bool has_aux(struct perf_event *event)
1148 {
1149 return event->pmu->setup_aux;
1150 }
1151
1152 static inline bool is_write_backward(struct perf_event *event)
1153 {
1154 return !!event->attr.write_backward;
1155 }
1156
1157 static inline bool has_addr_filter(struct perf_event *event)
1158 {
1159 return event->pmu->nr_addr_filters;
1160 }
1161
1162 /*
1163 * An inherited event uses parent's filters
1164 */
1165 static inline struct perf_addr_filters_head *
1166 perf_event_addr_filters(struct perf_event *event)
1167 {
1168 struct perf_addr_filters_head *ifh = &event->addr_filters;
1169
1170 if (event->parent)
1171 ifh = &event->parent->addr_filters;
1172
1173 return ifh;
1174 }
1175
1176 extern void perf_event_addr_filters_sync(struct perf_event *event);
1177
1178 extern int perf_output_begin(struct perf_output_handle *handle,
1179 struct perf_event *event, unsigned int size);
1180 extern int perf_output_begin_forward(struct perf_output_handle *handle,
1181 struct perf_event *event,
1182 unsigned int size);
1183 extern int perf_output_begin_backward(struct perf_output_handle *handle,
1184 struct perf_event *event,
1185 unsigned int size);
1186
1187 extern void perf_output_end(struct perf_output_handle *handle);
1188 extern unsigned int perf_output_copy(struct perf_output_handle *handle,
1189 const void *buf, unsigned int len);
1190 extern unsigned int perf_output_skip(struct perf_output_handle *handle,
1191 unsigned int len);
1192 extern int perf_swevent_get_recursion_context(void);
1193 extern void perf_swevent_put_recursion_context(int rctx);
1194 extern u64 perf_swevent_set_period(struct perf_event *event);
1195 extern void perf_event_enable(struct perf_event *event);
1196 extern void perf_event_disable(struct perf_event *event);
1197 extern void perf_event_disable_local(struct perf_event *event);
1198 extern void perf_event_task_tick(void);
1199 #else /* !CONFIG_PERF_EVENTS: */
1200 static inline void *
1201 perf_aux_output_begin(struct perf_output_handle *handle,
1202 struct perf_event *event) { return NULL; }
1203 static inline void
1204 perf_aux_output_end(struct perf_output_handle *handle, unsigned long size,
1205 bool truncated) { }
1206 static inline int
1207 perf_aux_output_skip(struct perf_output_handle *handle,
1208 unsigned long size) { return -EINVAL; }
1209 static inline void *
1210 perf_get_aux(struct perf_output_handle *handle) { return NULL; }
1211 static inline void
1212 perf_event_task_migrate(struct task_struct *task) { }
1213 static inline void
1214 perf_event_task_sched_in(struct task_struct *prev,
1215 struct task_struct *task) { }
1216 static inline void
1217 perf_event_task_sched_out(struct task_struct *prev,
1218 struct task_struct *next) { }
1219 static inline int perf_event_init_task(struct task_struct *child) { return 0; }
1220 static inline void perf_event_exit_task(struct task_struct *child) { }
1221 static inline void perf_event_free_task(struct task_struct *task) { }
1222 static inline void perf_event_delayed_put(struct task_struct *task) { }
1223 static inline struct file *perf_event_get(unsigned int fd) { return ERR_PTR(-EINVAL); }
1224 static inline const struct perf_event_attr *perf_event_attrs(struct perf_event *event)
1225 {
1226 return ERR_PTR(-EINVAL);
1227 }
1228 static inline u64 perf_event_read_local(struct perf_event *event) { return -EINVAL; }
1229 static inline void perf_event_print_debug(void) { }
1230 static inline int perf_event_task_disable(void) { return -EINVAL; }
1231 static inline int perf_event_task_enable(void) { return -EINVAL; }
1232 static inline int perf_event_refresh(struct perf_event *event, int refresh)
1233 {
1234 return -EINVAL;
1235 }
1236
1237 static inline void
1238 perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr) { }
1239 static inline void
1240 perf_sw_event_sched(u32 event_id, u64 nr, u64 addr) { }
1241 static inline void
1242 perf_bp_event(struct perf_event *event, void *data) { }
1243
1244 static inline int perf_register_guest_info_callbacks
1245 (struct perf_guest_info_callbacks *callbacks) { return 0; }
1246 static inline int perf_unregister_guest_info_callbacks
1247 (struct perf_guest_info_callbacks *callbacks) { return 0; }
1248
1249 static inline void perf_event_mmap(struct vm_area_struct *vma) { }
1250 static inline void perf_event_exec(void) { }
1251 static inline void perf_event_comm(struct task_struct *tsk, bool exec) { }
1252 static inline void perf_event_fork(struct task_struct *tsk) { }
1253 static inline void perf_event_init(void) { }
1254 static inline int perf_swevent_get_recursion_context(void) { return -1; }
1255 static inline void perf_swevent_put_recursion_context(int rctx) { }
1256 static inline u64 perf_swevent_set_period(struct perf_event *event) { return 0; }
1257 static inline void perf_event_enable(struct perf_event *event) { }
1258 static inline void perf_event_disable(struct perf_event *event) { }
1259 static inline int __perf_event_disable(void *info) { return -1; }
1260 static inline void perf_event_task_tick(void) { }
1261 static inline int perf_event_release_kernel(struct perf_event *event) { return 0; }
1262 #endif
1263
1264 #if defined(CONFIG_PERF_EVENTS) && defined(CONFIG_CPU_SUP_INTEL)
1265 extern void perf_restore_debug_store(void);
1266 #else
1267 static inline void perf_restore_debug_store(void) { }
1268 #endif
1269
1270 #define perf_output_put(handle, x) perf_output_copy((handle), &(x), sizeof(x))
1271
1272 /*
1273 * This has to have a higher priority than migration_notifier in sched/core.c.
1274 */
1275 #define perf_cpu_notifier(fn) \
1276 do { \
1277 static struct notifier_block fn##_nb = \
1278 { .notifier_call = fn, .priority = CPU_PRI_PERF }; \
1279 unsigned long cpu = smp_processor_id(); \
1280 unsigned long flags; \
1281 \
1282 cpu_notifier_register_begin(); \
1283 fn(&fn##_nb, (unsigned long)CPU_UP_PREPARE, \
1284 (void *)(unsigned long)cpu); \
1285 local_irq_save(flags); \
1286 fn(&fn##_nb, (unsigned long)CPU_STARTING, \
1287 (void *)(unsigned long)cpu); \
1288 local_irq_restore(flags); \
1289 fn(&fn##_nb, (unsigned long)CPU_ONLINE, \
1290 (void *)(unsigned long)cpu); \
1291 __register_cpu_notifier(&fn##_nb); \
1292 cpu_notifier_register_done(); \
1293 } while (0)
1294
1295 /*
1296 * Bare-bones version of perf_cpu_notifier(), which doesn't invoke the
1297 * callback for already online CPUs.
1298 */
1299 #define __perf_cpu_notifier(fn) \
1300 do { \
1301 static struct notifier_block fn##_nb = \
1302 { .notifier_call = fn, .priority = CPU_PRI_PERF }; \
1303 \
1304 __register_cpu_notifier(&fn##_nb); \
1305 } while (0)
1306
1307 struct perf_pmu_events_attr {
1308 struct device_attribute attr;
1309 u64 id;
1310 const char *event_str;
1311 };
1312
1313 ssize_t perf_event_sysfs_show(struct device *dev, struct device_attribute *attr,
1314 char *page);
1315
1316 #define PMU_EVENT_ATTR(_name, _var, _id, _show) \
1317 static struct perf_pmu_events_attr _var = { \
1318 .attr = __ATTR(_name, 0444, _show, NULL), \
1319 .id = _id, \
1320 };
1321
1322 #define PMU_EVENT_ATTR_STRING(_name, _var, _str) \
1323 static struct perf_pmu_events_attr _var = { \
1324 .attr = __ATTR(_name, 0444, perf_event_sysfs_show, NULL), \
1325 .id = 0, \
1326 .event_str = _str, \
1327 };
1328
1329 #define PMU_FORMAT_ATTR(_name, _format) \
1330 static ssize_t \
1331 _name##_show(struct device *dev, \
1332 struct device_attribute *attr, \
1333 char *page) \
1334 { \
1335 BUILD_BUG_ON(sizeof(_format) >= PAGE_SIZE); \
1336 return sprintf(page, _format "\n"); \
1337 } \
1338 \
1339 static struct device_attribute format_attr_##_name = __ATTR_RO(_name)
1340
1341 #endif /* _LINUX_PERF_EVENT_H */
This page took 0.068363 seconds and 6 git commands to generate.