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