perf: Fix perf_poll to return proper POLLHUP value
[deliverable/linux.git] / include / linux / perf_event.h
CommitLineData
0793a61d 1/*
57c0c15b 2 * Performance events:
0793a61d 3 *
a308444c 4 * Copyright (C) 2008-2009, Thomas Gleixner <tglx@linutronix.de>
e7e7ee2e
IM
5 * Copyright (C) 2008-2011, Red Hat, Inc., Ingo Molnar
6 * Copyright (C) 2008-2011, Red Hat, Inc., Peter Zijlstra
0793a61d 7 *
57c0c15b 8 * Data type definitions, declarations, prototypes.
0793a61d 9 *
a308444c 10 * Started by: Thomas Gleixner and Ingo Molnar
0793a61d 11 *
57c0c15b 12 * For licencing details see kernel-base/COPYING
0793a61d 13 */
cdd6c482
IM
14#ifndef _LINUX_PERF_EVENT_H
15#define _LINUX_PERF_EVENT_H
0793a61d 16
607ca46e 17#include <uapi/linux/perf_event.h>
0793a61d 18
9f66a381 19/*
f3dfd265 20 * Kernel-internal data types and definitions:
9f66a381
IM
21 */
22
cdd6c482
IM
23#ifdef CONFIG_PERF_EVENTS
24# include <asm/perf_event.h>
7be79236 25# include <asm/local64.h>
f3dfd265
PM
26#endif
27
39447b38 28struct perf_guest_info_callbacks {
e7e7ee2e
IM
29 int (*is_in_guest)(void);
30 int (*is_user_mode)(void);
31 unsigned long (*get_guest_ip)(void);
39447b38
ZY
32};
33
2ff6cfd7
AB
34#ifdef CONFIG_HAVE_HW_BREAKPOINT
35#include <asm/hw_breakpoint.h>
36#endif
37
f3dfd265
PM
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>
d6d020e9 43#include <linux/hrtimer.h>
3c446b3d 44#include <linux/fs.h>
709e50cf 45#include <linux/pid_namespace.h>
906010b2 46#include <linux/workqueue.h>
5331d7b8 47#include <linux/ftrace.h>
85cfabbc 48#include <linux/cpu.h>
e360adbe 49#include <linux/irq_work.h>
c5905afb 50#include <linux/static_key.h>
851cf6e7 51#include <linux/jump_label_ratelimit.h>
60063497 52#include <linux/atomic.h>
641cc938 53#include <linux/sysfs.h>
4018994f 54#include <linux/perf_regs.h>
fadfe7be 55#include <linux/workqueue.h>
fa588151 56#include <asm/local.h>
f3dfd265 57
f9188e02
PZ
58struct perf_callchain_entry {
59 __u64 nr;
60 __u64 ip[PERF_MAX_STACK_DEPTH];
61};
62
3a43ce68
FW
63struct perf_raw_record {
64 u32 size;
65 void *data;
f413cdb8
FW
66};
67
bce38cd5
SE
68/*
69 * branch stack layout:
70 * nr: number of taken branches stored in entries[]
71 *
72 * Note that nr can vary from sample to sample
73 * branches (to, from) are stored from most recent
74 * to least recent, i.e., entries[0] contains the most
75 * recent branch.
76 */
caff2bef
PZ
77struct perf_branch_stack {
78 __u64 nr;
79 struct perf_branch_entry entries[0];
80};
81
4018994f
JO
82struct perf_regs_user {
83 __u64 abi;
84 struct pt_regs *regs;
85};
86
f3dfd265
PM
87struct task_struct;
88
efc9f05d
SE
89/*
90 * extra PMU register associated with an event
91 */
92struct hw_perf_event_extra {
93 u64 config; /* register value */
94 unsigned int reg; /* register address or index */
95 int alloc; /* extra register already allocated */
96 int idx; /* index in shared_regs->regs[] */
97};
98
43b45780
AH
99struct event_constraint;
100
0793a61d 101/**
cdd6c482 102 * struct hw_perf_event - performance event hardware details:
0793a61d 103 */
cdd6c482
IM
104struct hw_perf_event {
105#ifdef CONFIG_PERF_EVENTS
d6d020e9
PZ
106 union {
107 struct { /* hardware */
a308444c 108 u64 config;
447a194b 109 u64 last_tag;
a308444c 110 unsigned long config_base;
cdd6c482 111 unsigned long event_base;
c48b6053 112 int event_base_rdpmc;
a308444c 113 int idx;
447a194b 114 int last_cpu;
9fac2cf3 115 int flags;
bce38cd5 116
efc9f05d 117 struct hw_perf_event_extra extra_reg;
bce38cd5 118 struct hw_perf_event_extra branch_reg;
43b45780
AH
119
120 struct event_constraint *constraint;
d6d020e9 121 };
721a669b 122 struct { /* software */
a308444c 123 struct hrtimer hrtimer;
d6d020e9 124 };
f22c1bb6
ON
125 struct { /* tracepoint */
126 struct task_struct *tp_target;
127 /* for tp_event->class */
128 struct list_head tp_list;
129 };
24f1e32c 130#ifdef CONFIG_HAVE_HW_BREAKPOINT
45a73372 131 struct { /* breakpoint */
d580ff86
PZ
132 /*
133 * Crufty hack to avoid the chicken and egg
134 * problem hw_breakpoint has with context
135 * creation and event initalization.
136 */
137 struct task_struct *bp_target;
f22c1bb6
ON
138 struct arch_hw_breakpoint info;
139 struct list_head bp_list;
45a73372 140 };
24f1e32c 141#endif
d6d020e9 142 };
a4eaf7f1 143 int state;
e7850595 144 local64_t prev_count;
b23f3325 145 u64 sample_period;
9e350de3 146 u64 last_period;
e7850595 147 local64_t period_left;
e050e3f0 148 u64 interrupts_seq;
60db5e09 149 u64 interrupts;
6a24ed6c 150
abd50713
PZ
151 u64 freq_time_stamp;
152 u64 freq_count_stamp;
ee06094f 153#endif
0793a61d
TG
154};
155
a4eaf7f1
PZ
156/*
157 * hw_perf_event::state flags
158 */
159#define PERF_HES_STOPPED 0x01 /* the counter is stopped */
160#define PERF_HES_UPTODATE 0x02 /* event->count up-to-date */
161#define PERF_HES_ARCH 0x04
162
cdd6c482 163struct perf_event;
621a01ea 164
8d2cacbb
PZ
165/*
166 * Common implementation detail of pmu::{start,commit,cancel}_txn
167 */
168#define PERF_EVENT_TXN 0x1
6bde9b6c 169
53b25335
VW
170/**
171 * pmu::capabilities flags
172 */
173#define PERF_PMU_CAP_NO_INTERRUPT 0x01
174
621a01ea 175/**
4aeb0b42 176 * struct pmu - generic performance monitoring unit
621a01ea 177 */
4aeb0b42 178struct pmu {
b0a873eb
PZ
179 struct list_head entry;
180
c464c76e 181 struct module *module;
abe43400 182 struct device *dev;
0c9d42ed 183 const struct attribute_group **attr_groups;
03d8e80b 184 const char *name;
2e80a82a
PZ
185 int type;
186
53b25335
VW
187 /*
188 * various common per-pmu feature flags
189 */
190 int capabilities;
191
108b02cf
PZ
192 int * __percpu pmu_disable_count;
193 struct perf_cpu_context * __percpu pmu_cpu_context;
8dc85d54 194 int task_ctx_nr;
62b85639 195 int hrtimer_interval_ms;
6bde9b6c
LM
196
197 /*
a4eaf7f1
PZ
198 * Fully disable/enable this PMU, can be used to protect from the PMI
199 * as well as for lazy/batch writing of the MSRs.
6bde9b6c 200 */
ad5133b7
PZ
201 void (*pmu_enable) (struct pmu *pmu); /* optional */
202 void (*pmu_disable) (struct pmu *pmu); /* optional */
6bde9b6c 203
8d2cacbb 204 /*
a4eaf7f1 205 * Try and initialize the event for this PMU.
24cd7f54 206 * Should return -ENOENT when the @event doesn't match this PMU.
8d2cacbb 207 */
b0a873eb
PZ
208 int (*event_init) (struct perf_event *event);
209
a4eaf7f1
PZ
210#define PERF_EF_START 0x01 /* start the counter when adding */
211#define PERF_EF_RELOAD 0x02 /* reload the counter when starting */
212#define PERF_EF_UPDATE 0x04 /* update the counter when stopping */
213
8d2cacbb 214 /*
a4eaf7f1
PZ
215 * Adds/Removes a counter to/from the PMU, can be done inside
216 * a transaction, see the ->*_txn() methods.
217 */
218 int (*add) (struct perf_event *event, int flags);
219 void (*del) (struct perf_event *event, int flags);
220
221 /*
222 * Starts/Stops a counter present on the PMU. The PMI handler
223 * should stop the counter when perf_event_overflow() returns
224 * !0. ->start() will be used to continue.
225 */
226 void (*start) (struct perf_event *event, int flags);
227 void (*stop) (struct perf_event *event, int flags);
228
229 /*
230 * Updates the counter value of the event.
231 */
cdd6c482 232 void (*read) (struct perf_event *event);
6bde9b6c
LM
233
234 /*
24cd7f54
PZ
235 * Group events scheduling is treated as a transaction, add
236 * group events as a whole and perform one schedulability test.
237 * If the test fails, roll back the whole group
a4eaf7f1
PZ
238 *
239 * Start the transaction, after this ->add() doesn't need to
24cd7f54 240 * do schedulability tests.
8d2cacbb 241 */
e7e7ee2e 242 void (*start_txn) (struct pmu *pmu); /* optional */
8d2cacbb 243 /*
a4eaf7f1 244 * If ->start_txn() disabled the ->add() schedulability test
8d2cacbb
PZ
245 * then ->commit_txn() is required to perform one. On success
246 * the transaction is closed. On error the transaction is kept
247 * open until ->cancel_txn() is called.
248 */
e7e7ee2e 249 int (*commit_txn) (struct pmu *pmu); /* optional */
8d2cacbb 250 /*
a4eaf7f1 251 * Will cancel the transaction, assumes ->del() is called
25985edc 252 * for each successful ->add() during the transaction.
8d2cacbb 253 */
e7e7ee2e 254 void (*cancel_txn) (struct pmu *pmu); /* optional */
35edc2a5
PZ
255
256 /*
257 * Will return the value for perf_event_mmap_page::index for this event,
258 * if no implementation is provided it will default to: event->hw.idx + 1.
259 */
260 int (*event_idx) (struct perf_event *event); /*optional */
d010b332
SE
261
262 /*
263 * flush branch stack on context-switches (needed in cpu-wide mode)
264 */
265 void (*flush_branch_stack) (void);
621a01ea
IM
266};
267
6a930700 268/**
cdd6c482 269 * enum perf_event_active_state - the states of a event
6a930700 270 */
cdd6c482 271enum perf_event_active_state {
57c0c15b 272 PERF_EVENT_STATE_ERROR = -2,
cdd6c482
IM
273 PERF_EVENT_STATE_OFF = -1,
274 PERF_EVENT_STATE_INACTIVE = 0,
57c0c15b 275 PERF_EVENT_STATE_ACTIVE = 1,
6a930700
IM
276};
277
9b51f66d 278struct file;
453f19ee
PZ
279struct perf_sample_data;
280
a8b0ca17 281typedef void (*perf_overflow_handler_t)(struct perf_event *,
b326e956
FW
282 struct perf_sample_data *,
283 struct pt_regs *regs);
284
d6f962b5 285enum perf_group_flag {
e7e7ee2e 286 PERF_GROUP_SOFTWARE = 0x1,
d6f962b5
FW
287};
288
e7e7ee2e
IM
289#define SWEVENT_HLIST_BITS 8
290#define SWEVENT_HLIST_SIZE (1 << SWEVENT_HLIST_BITS)
76e1d904
FW
291
292struct swevent_hlist {
e7e7ee2e
IM
293 struct hlist_head heads[SWEVENT_HLIST_SIZE];
294 struct rcu_head rcu_head;
76e1d904
FW
295};
296
8a49542c
PZ
297#define PERF_ATTACH_CONTEXT 0x01
298#define PERF_ATTACH_GROUP 0x02
d580ff86 299#define PERF_ATTACH_TASK 0x04
8a49542c 300
877c6856 301struct perf_cgroup;
76369139
FW
302struct ring_buffer;
303
0793a61d 304/**
cdd6c482 305 * struct perf_event - performance event kernel representation:
0793a61d 306 */
cdd6c482
IM
307struct perf_event {
308#ifdef CONFIG_PERF_EVENTS
9886167d
PZ
309 /*
310 * entry onto perf_event_context::event_list;
311 * modifications require ctx->lock
312 * RCU safe iterations.
313 */
592903cd 314 struct list_head event_entry;
9886167d
PZ
315
316 /*
317 * XXX: group_entry and sibling_list should be mutually exclusive;
318 * either you're a sibling on a group, or you're the group leader.
319 * Rework the code to always use the same list element.
320 *
321 * Locked for modification by both ctx->mutex and ctx->lock; holding
322 * either sufficies for read.
323 */
324 struct list_head group_entry;
04289bb9 325 struct list_head sibling_list;
9886167d
PZ
326
327 /*
328 * We need storage to track the entries in perf_pmu_migrate_context; we
329 * cannot use the event_entry because of RCU and we want to keep the
330 * group in tact which avoids us using the other two entries.
331 */
332 struct list_head migrate_entry;
333
f3ae75de
SE
334 struct hlist_node hlist_entry;
335 struct list_head active_entry;
0127c3ea 336 int nr_siblings;
d6f962b5 337 int group_flags;
cdd6c482 338 struct perf_event *group_leader;
a4eaf7f1 339 struct pmu *pmu;
04289bb9 340
cdd6c482 341 enum perf_event_active_state state;
8a49542c 342 unsigned int attach_state;
e7850595 343 local64_t count;
a6e6dea6 344 atomic64_t child_count;
ee06094f 345
53cfbf59 346 /*
cdd6c482 347 * These are the total time in nanoseconds that the event
53cfbf59 348 * has been enabled (i.e. eligible to run, and the task has
cdd6c482 349 * been scheduled in, if this is a per-task event)
53cfbf59
PM
350 * and running (scheduled onto the CPU), respectively.
351 *
352 * They are computed from tstamp_enabled, tstamp_running and
cdd6c482 353 * tstamp_stopped when the event is in INACTIVE or ACTIVE state.
53cfbf59
PM
354 */
355 u64 total_time_enabled;
356 u64 total_time_running;
357
358 /*
359 * These are timestamps used for computing total_time_enabled
cdd6c482 360 * and total_time_running when the event is in INACTIVE or
53cfbf59
PM
361 * ACTIVE state, measured in nanoseconds from an arbitrary point
362 * in time.
cdd6c482
IM
363 * tstamp_enabled: the notional time when the event was enabled
364 * tstamp_running: the notional time when the event was scheduled on
53cfbf59 365 * tstamp_stopped: in INACTIVE state, the notional time when the
cdd6c482 366 * event was scheduled off.
53cfbf59
PM
367 */
368 u64 tstamp_enabled;
369 u64 tstamp_running;
370 u64 tstamp_stopped;
371
eed01528
SE
372 /*
373 * timestamp shadows the actual context timing but it can
374 * be safely used in NMI interrupt context. It reflects the
375 * context time as it was when the event was last scheduled in.
376 *
377 * ctx_time already accounts for ctx->timestamp. Therefore to
378 * compute ctx_time for a sample, simply add perf_clock().
379 */
380 u64 shadow_ctx_time;
381
24f1e32c 382 struct perf_event_attr attr;
c320c7b7 383 u16 header_size;
6844c09d 384 u16 id_header_size;
c320c7b7 385 u16 read_size;
cdd6c482 386 struct hw_perf_event hw;
0793a61d 387
cdd6c482 388 struct perf_event_context *ctx;
a6fa941d 389 atomic_long_t refcount;
0793a61d 390
53cfbf59
PM
391 /*
392 * These accumulate total time (in nanoseconds) that children
cdd6c482 393 * events have been enabled and running, respectively.
53cfbf59
PM
394 */
395 atomic64_t child_total_time_enabled;
396 atomic64_t child_total_time_running;
397
0793a61d 398 /*
d859e29f 399 * Protect attach/detach and child_list:
0793a61d 400 */
fccc714b
PZ
401 struct mutex child_mutex;
402 struct list_head child_list;
cdd6c482 403 struct perf_event *parent;
0793a61d
TG
404
405 int oncpu;
406 int cpu;
407
082ff5a2
PZ
408 struct list_head owner_entry;
409 struct task_struct *owner;
410
7b732a75
PZ
411 /* mmap bits */
412 struct mutex mmap_mutex;
413 atomic_t mmap_count;
26cb63ad 414
76369139 415 struct ring_buffer *rb;
10c6db11 416 struct list_head rb_entry;
b69cf536
PZ
417 unsigned long rcu_batches;
418 int rcu_pending;
37d81828 419
7b732a75 420 /* poll related */
0793a61d 421 wait_queue_head_t waitq;
3c446b3d 422 struct fasync_struct *fasync;
79f14641
PZ
423
424 /* delayed work for NMIs and such */
425 int pending_wakeup;
4c9e2542 426 int pending_kill;
79f14641 427 int pending_disable;
e360adbe 428 struct irq_work pending;
592903cd 429
79f14641
PZ
430 atomic_t event_limit;
431
cdd6c482 432 void (*destroy)(struct perf_event *);
592903cd 433 struct rcu_head rcu_head;
709e50cf
PZ
434
435 struct pid_namespace *ns;
8e5799b1 436 u64 id;
6fb2915d 437
b326e956 438 perf_overflow_handler_t overflow_handler;
4dc0da86 439 void *overflow_handler_context;
453f19ee 440
07b139c8 441#ifdef CONFIG_EVENT_TRACING
1c024eca 442 struct ftrace_event_call *tp_event;
6fb2915d 443 struct event_filter *filter;
ced39002
JO
444#ifdef CONFIG_FUNCTION_TRACER
445 struct ftrace_ops ftrace_ops;
446#endif
ee06094f 447#endif
6fb2915d 448
e5d1367f
SE
449#ifdef CONFIG_CGROUP_PERF
450 struct perf_cgroup *cgrp; /* cgroup event is attach to */
451 int cgrp_defer_enabled;
452#endif
453
6fb2915d 454#endif /* CONFIG_PERF_EVENTS */
0793a61d
TG
455};
456
b04243ef
PZ
457enum perf_event_context_type {
458 task_context,
459 cpu_context,
460};
461
0793a61d 462/**
cdd6c482 463 * struct perf_event_context - event context structure
0793a61d 464 *
cdd6c482 465 * Used as a container for task events and CPU events as well:
0793a61d 466 */
cdd6c482 467struct perf_event_context {
108b02cf 468 struct pmu *pmu;
ee643c41 469 enum perf_event_context_type type;
0793a61d 470 /*
cdd6c482 471 * Protect the states of the events in the list,
d859e29f 472 * nr_active, and the list:
0793a61d 473 */
e625cce1 474 raw_spinlock_t lock;
d859e29f 475 /*
cdd6c482 476 * Protect the list of events. Locking either mutex or lock
d859e29f
PM
477 * is sufficient to ensure the list doesn't change; to change
478 * the list you need to lock both the mutex and the spinlock.
479 */
a308444c 480 struct mutex mutex;
04289bb9 481
889ff015
FW
482 struct list_head pinned_groups;
483 struct list_head flexible_groups;
a308444c 484 struct list_head event_list;
cdd6c482 485 int nr_events;
a308444c
IM
486 int nr_active;
487 int is_active;
bfbd3381 488 int nr_stat;
0f5a2601 489 int nr_freq;
dddd3379 490 int rotate_disable;
a308444c
IM
491 atomic_t refcount;
492 struct task_struct *task;
53cfbf59
PM
493
494 /*
4af4998b 495 * Context clock, runs when context enabled.
53cfbf59 496 */
a308444c
IM
497 u64 time;
498 u64 timestamp;
564c2b21
PM
499
500 /*
501 * These fields let us detect when two contexts have both
502 * been cloned (inherited) from a common ancestor.
503 */
cdd6c482 504 struct perf_event_context *parent_ctx;
a308444c
IM
505 u64 parent_gen;
506 u64 generation;
507 int pin_count;
d010b332
SE
508 int nr_cgroups; /* cgroup evts */
509 int nr_branch_stack; /* branch_stack evt */
28009ce4 510 struct rcu_head rcu_head;
fadfe7be
JO
511
512 struct delayed_work orphans_remove;
513 bool orphans_remove_sched;
0793a61d
TG
514};
515
7ae07ea3
FW
516/*
517 * Number of contexts where an event can trigger:
e7e7ee2e 518 * task, softirq, hardirq, nmi.
7ae07ea3
FW
519 */
520#define PERF_NR_CONTEXTS 4
521
0793a61d 522/**
cdd6c482 523 * struct perf_event_cpu_context - per cpu event context structure
0793a61d
TG
524 */
525struct perf_cpu_context {
cdd6c482
IM
526 struct perf_event_context ctx;
527 struct perf_event_context *task_ctx;
0793a61d 528 int active_oncpu;
3b6f9e5c 529 int exclusive;
9e630205
SE
530 struct hrtimer hrtimer;
531 ktime_t hrtimer_interval;
e9d2b064 532 struct list_head rotation_list;
3f1f3320 533 struct pmu *unique_pmu;
e5d1367f 534 struct perf_cgroup *cgrp;
0793a61d
TG
535};
536
5622f295 537struct perf_output_handle {
57c0c15b 538 struct perf_event *event;
76369139 539 struct ring_buffer *rb;
6d1acfd5 540 unsigned long wakeup;
5d967a8b
PZ
541 unsigned long size;
542 void *addr;
543 int page;
5622f295
MM
544};
545
cdd6c482 546#ifdef CONFIG_PERF_EVENTS
829b42dd 547
03d8e80b 548extern int perf_pmu_register(struct pmu *pmu, const char *name, int type);
b0a873eb 549extern void perf_pmu_unregister(struct pmu *pmu);
621a01ea 550
3bf101ba 551extern int perf_num_counters(void);
84c79910 552extern const char *perf_pmu_name(void);
ab0cce56
JO
553extern void __perf_event_task_sched_in(struct task_struct *prev,
554 struct task_struct *task);
555extern void __perf_event_task_sched_out(struct task_struct *prev,
556 struct task_struct *next);
cdd6c482
IM
557extern int perf_event_init_task(struct task_struct *child);
558extern void perf_event_exit_task(struct task_struct *child);
559extern void perf_event_free_task(struct task_struct *task);
4e231c79 560extern void perf_event_delayed_put(struct task_struct *task);
cdd6c482 561extern void perf_event_print_debug(void);
33696fc0
PZ
562extern void perf_pmu_disable(struct pmu *pmu);
563extern void perf_pmu_enable(struct pmu *pmu);
cdd6c482
IM
564extern int perf_event_task_disable(void);
565extern int perf_event_task_enable(void);
26ca5c11 566extern int perf_event_refresh(struct perf_event *event, int refresh);
cdd6c482 567extern void perf_event_update_userpage(struct perf_event *event);
fb0459d7
AV
568extern int perf_event_release_kernel(struct perf_event *event);
569extern struct perf_event *
570perf_event_create_kernel_counter(struct perf_event_attr *attr,
571 int cpu,
38a81da2 572 struct task_struct *task,
4dc0da86
AK
573 perf_overflow_handler_t callback,
574 void *context);
0cda4c02
YZ
575extern void perf_pmu_migrate_context(struct pmu *pmu,
576 int src_cpu, int dst_cpu);
59ed446f
PZ
577extern u64 perf_event_read_value(struct perf_event *event,
578 u64 *enabled, u64 *running);
5c92d124 579
d010b332 580
df1a132b 581struct perf_sample_data {
5622f295
MM
582 u64 type;
583
584 u64 ip;
585 struct {
586 u32 pid;
587 u32 tid;
588 } tid_entry;
589 u64 time;
a308444c 590 u64 addr;
5622f295
MM
591 u64 id;
592 u64 stream_id;
593 struct {
594 u32 cpu;
595 u32 reserved;
596 } cpu_entry;
a308444c 597 u64 period;
d6be9ad6 598 union perf_mem_data_src data_src;
5622f295 599 struct perf_callchain_entry *callchain;
3a43ce68 600 struct perf_raw_record *raw;
bce38cd5 601 struct perf_branch_stack *br_stack;
4018994f 602 struct perf_regs_user regs_user;
c5ebcedb 603 u64 stack_user_size;
c3feedf2 604 u64 weight;
fdfbbd07
AK
605 /*
606 * Transaction flags for abort events:
607 */
608 u64 txn;
df1a132b
PZ
609};
610
770eee1f
SE
611/* default value for data source */
612#define PERF_MEM_NA (PERF_MEM_S(OP, NA) |\
613 PERF_MEM_S(LVL, NA) |\
614 PERF_MEM_S(SNOOP, NA) |\
615 PERF_MEM_S(LOCK, NA) |\
616 PERF_MEM_S(TLB, NA))
617
fd0d000b
RR
618static inline void perf_sample_data_init(struct perf_sample_data *data,
619 u64 addr, u64 period)
dc1d628a 620{
fd0d000b 621 /* remaining struct members initialized in perf_prepare_sample() */
dc1d628a
PZ
622 data->addr = addr;
623 data->raw = NULL;
bce38cd5 624 data->br_stack = NULL;
4018994f
JO
625 data->period = period;
626 data->regs_user.abi = PERF_SAMPLE_REGS_ABI_NONE;
627 data->regs_user.regs = NULL;
c5ebcedb 628 data->stack_user_size = 0;
c3feedf2 629 data->weight = 0;
770eee1f 630 data->data_src.val = PERF_MEM_NA;
fdfbbd07 631 data->txn = 0;
dc1d628a
PZ
632}
633
5622f295
MM
634extern void perf_output_sample(struct perf_output_handle *handle,
635 struct perf_event_header *header,
636 struct perf_sample_data *data,
cdd6c482 637 struct perf_event *event);
5622f295
MM
638extern void perf_prepare_sample(struct perf_event_header *header,
639 struct perf_sample_data *data,
cdd6c482 640 struct perf_event *event,
5622f295
MM
641 struct pt_regs *regs);
642
a8b0ca17 643extern int perf_event_overflow(struct perf_event *event,
5622f295
MM
644 struct perf_sample_data *data,
645 struct pt_regs *regs);
df1a132b 646
6c7e550f
FBH
647static inline bool is_sampling_event(struct perf_event *event)
648{
649 return event->attr.sample_period != 0;
650}
651
3b6f9e5c 652/*
cdd6c482 653 * Return 1 for a software event, 0 for a hardware event
3b6f9e5c 654 */
cdd6c482 655static inline int is_software_event(struct perf_event *event)
3b6f9e5c 656{
89a1e187 657 return event->pmu->task_ctx_nr == perf_sw_context;
3b6f9e5c
PM
658}
659
c5905afb 660extern struct static_key perf_swevent_enabled[PERF_COUNT_SW_MAX];
f29ac756 661
a8b0ca17 662extern void __perf_sw_event(u32, u64, struct pt_regs *, u64);
f29ac756 663
b0f82b81 664#ifndef perf_arch_fetch_caller_regs
e7e7ee2e 665static inline void perf_arch_fetch_caller_regs(struct pt_regs *regs, unsigned long ip) { }
b0f82b81 666#endif
5331d7b8
FW
667
668/*
669 * Take a snapshot of the regs. Skip ip and frame pointer to
670 * the nth caller. We only need a few of the regs:
671 * - ip for PERF_SAMPLE_IP
672 * - cs for user_mode() tests
673 * - bp for callchains
674 * - eflags, for future purposes, just in case
675 */
b0f82b81 676static inline void perf_fetch_caller_regs(struct pt_regs *regs)
5331d7b8 677{
5331d7b8
FW
678 memset(regs, 0, sizeof(*regs));
679
b0f82b81 680 perf_arch_fetch_caller_regs(regs, CALLER_ADDR0);
5331d7b8
FW
681}
682
7e54a5a0 683static __always_inline void
a8b0ca17 684perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr)
e49a5bd3 685{
7e54a5a0
PZ
686 struct pt_regs hot_regs;
687
c5905afb 688 if (static_key_false(&perf_swevent_enabled[event_id])) {
d430d3d7
JB
689 if (!regs) {
690 perf_fetch_caller_regs(&hot_regs);
691 regs = &hot_regs;
692 }
a8b0ca17 693 __perf_sw_event(event_id, nr, regs, addr);
e49a5bd3
FW
694 }
695}
696
c5905afb 697extern struct static_key_deferred perf_sched_events;
ee6dcfa4 698
ab0cce56 699static inline void perf_event_task_sched_in(struct task_struct *prev,
a8d757ef 700 struct task_struct *task)
ab0cce56
JO
701{
702 if (static_key_false(&perf_sched_events.key))
703 __perf_event_task_sched_in(prev, task);
704}
705
706static inline void perf_event_task_sched_out(struct task_struct *prev,
707 struct task_struct *next)
ee6dcfa4 708{
a8b0ca17 709 perf_sw_event(PERF_COUNT_SW_CONTEXT_SWITCHES, 1, NULL, 0);
ee6dcfa4 710
c5905afb 711 if (static_key_false(&perf_sched_events.key))
ab0cce56 712 __perf_event_task_sched_out(prev, next);
ee6dcfa4
PZ
713}
714
3af9e859 715extern void perf_event_mmap(struct vm_area_struct *vma);
39447b38 716extern struct perf_guest_info_callbacks *perf_guest_cbs;
dcf46b94
ZY
717extern int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *callbacks);
718extern int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *callbacks);
39447b38 719
e041e328 720extern void perf_event_exec(void);
82b89778 721extern void perf_event_comm(struct task_struct *tsk, bool exec);
cdd6c482 722extern void perf_event_fork(struct task_struct *tsk);
8d1b2d93 723
56962b44
FW
724/* Callchains */
725DECLARE_PER_CPU(struct perf_callchain_entry, perf_callchain_entry);
726
e7e7ee2e
IM
727extern void perf_callchain_user(struct perf_callchain_entry *entry, struct pt_regs *regs);
728extern void perf_callchain_kernel(struct perf_callchain_entry *entry, struct pt_regs *regs);
394ee076 729
e7e7ee2e 730static inline void perf_callchain_store(struct perf_callchain_entry *entry, u64 ip)
70791ce9
FW
731{
732 if (entry->nr < PERF_MAX_STACK_DEPTH)
733 entry->ip[entry->nr++] = ip;
734}
394ee076 735
cdd6c482
IM
736extern int sysctl_perf_event_paranoid;
737extern int sysctl_perf_event_mlock;
738extern int sysctl_perf_event_sample_rate;
14c63f17
DH
739extern int sysctl_perf_cpu_time_max_percent;
740
741extern void perf_sample_event_took(u64 sample_len_ns);
1ccd1549 742
163ec435
PZ
743extern int perf_proc_update_handler(struct ctl_table *table, int write,
744 void __user *buffer, size_t *lenp,
745 loff_t *ppos);
14c63f17
DH
746extern int perf_cpu_time_max_percent_handler(struct ctl_table *table, int write,
747 void __user *buffer, size_t *lenp,
748 loff_t *ppos);
749
163ec435 750
320ebf09
PZ
751static inline bool perf_paranoid_tracepoint_raw(void)
752{
753 return sysctl_perf_event_paranoid > -1;
754}
755
756static inline bool perf_paranoid_cpu(void)
757{
758 return sysctl_perf_event_paranoid > 0;
759}
760
761static inline bool perf_paranoid_kernel(void)
762{
763 return sysctl_perf_event_paranoid > 1;
764}
765
cdd6c482 766extern void perf_event_init(void);
1c024eca
PZ
767extern void perf_tp_event(u64 addr, u64 count, void *record,
768 int entry_size, struct pt_regs *regs,
e6dab5ff
AV
769 struct hlist_head *head, int rctx,
770 struct task_struct *task);
24f1e32c 771extern void perf_bp_event(struct perf_event *event, void *data);
0d905bca 772
9d23a90a 773#ifndef perf_misc_flags
e7e7ee2e
IM
774# define perf_misc_flags(regs) \
775 (user_mode(regs) ? PERF_RECORD_MISC_USER : PERF_RECORD_MISC_KERNEL)
776# define perf_instruction_pointer(regs) instruction_pointer(regs)
9d23a90a
PM
777#endif
778
bce38cd5
SE
779static inline bool has_branch_stack(struct perf_event *event)
780{
781 return event->attr.sample_type & PERF_SAMPLE_BRANCH_STACK;
782}
783
5622f295 784extern int perf_output_begin(struct perf_output_handle *handle,
a7ac67ea 785 struct perf_event *event, unsigned int size);
5622f295 786extern void perf_output_end(struct perf_output_handle *handle);
91d7753a 787extern unsigned int perf_output_copy(struct perf_output_handle *handle,
5622f295 788 const void *buf, unsigned int len);
5685e0ff
JO
789extern unsigned int perf_output_skip(struct perf_output_handle *handle,
790 unsigned int len);
4ed7c92d
PZ
791extern int perf_swevent_get_recursion_context(void);
792extern void perf_swevent_put_recursion_context(int rctx);
ab573844 793extern u64 perf_swevent_set_period(struct perf_event *event);
44234adc
FW
794extern void perf_event_enable(struct perf_event *event);
795extern void perf_event_disable(struct perf_event *event);
500ad2d8 796extern int __perf_event_disable(void *info);
e9d2b064 797extern void perf_event_task_tick(void);
e041e328 798#else /* !CONFIG_PERF_EVENTS: */
0793a61d 799static inline void
ab0cce56
JO
800perf_event_task_sched_in(struct task_struct *prev,
801 struct task_struct *task) { }
802static inline void
803perf_event_task_sched_out(struct task_struct *prev,
804 struct task_struct *next) { }
cdd6c482
IM
805static inline int perf_event_init_task(struct task_struct *child) { return 0; }
806static inline void perf_event_exit_task(struct task_struct *child) { }
807static inline void perf_event_free_task(struct task_struct *task) { }
4e231c79 808static inline void perf_event_delayed_put(struct task_struct *task) { }
57c0c15b 809static inline void perf_event_print_debug(void) { }
57c0c15b
IM
810static inline int perf_event_task_disable(void) { return -EINVAL; }
811static inline int perf_event_task_enable(void) { return -EINVAL; }
26ca5c11
AK
812static inline int perf_event_refresh(struct perf_event *event, int refresh)
813{
814 return -EINVAL;
815}
15dbf27c 816
925d519a 817static inline void
a8b0ca17 818perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr) { }
24f1e32c 819static inline void
184f412c 820perf_bp_event(struct perf_event *event, void *data) { }
0a4a9391 821
39447b38 822static inline int perf_register_guest_info_callbacks
e7e7ee2e 823(struct perf_guest_info_callbacks *callbacks) { return 0; }
39447b38 824static inline int perf_unregister_guest_info_callbacks
e7e7ee2e 825(struct perf_guest_info_callbacks *callbacks) { return 0; }
39447b38 826
57c0c15b 827static inline void perf_event_mmap(struct vm_area_struct *vma) { }
e041e328 828static inline void perf_event_exec(void) { }
82b89778 829static inline void perf_event_comm(struct task_struct *tsk, bool exec) { }
cdd6c482
IM
830static inline void perf_event_fork(struct task_struct *tsk) { }
831static inline void perf_event_init(void) { }
184f412c 832static inline int perf_swevent_get_recursion_context(void) { return -1; }
4ed7c92d 833static inline void perf_swevent_put_recursion_context(int rctx) { }
ab573844 834static inline u64 perf_swevent_set_period(struct perf_event *event) { return 0; }
44234adc
FW
835static inline void perf_event_enable(struct perf_event *event) { }
836static inline void perf_event_disable(struct perf_event *event) { }
500ad2d8 837static inline int __perf_event_disable(void *info) { return -1; }
e9d2b064 838static inline void perf_event_task_tick(void) { }
0793a61d
TG
839#endif
840
026249ef
FW
841#if defined(CONFIG_PERF_EVENTS) && defined(CONFIG_NO_HZ_FULL)
842extern bool perf_event_can_stop_tick(void);
843#else
844static inline bool perf_event_can_stop_tick(void) { return true; }
845#endif
846
6c4d3bc9
DR
847#if defined(CONFIG_PERF_EVENTS) && defined(CONFIG_CPU_SUP_INTEL)
848extern void perf_restore_debug_store(void);
849#else
1d9d8639 850static inline void perf_restore_debug_store(void) { }
0793a61d
TG
851#endif
852
e7e7ee2e 853#define perf_output_put(handle, x) perf_output_copy((handle), &(x), sizeof(x))
5622f295 854
3f6da390 855/*
0a0fca9d 856 * This has to have a higher priority than migration_notifier in sched/core.c.
3f6da390 857 */
e7e7ee2e
IM
858#define perf_cpu_notifier(fn) \
859do { \
0db0628d 860 static struct notifier_block fn##_nb = \
e7e7ee2e 861 { .notifier_call = fn, .priority = CPU_PRI_PERF }; \
c13d38e4 862 unsigned long cpu = smp_processor_id(); \
6760bca9 863 unsigned long flags; \
f0bdb5e0
SB
864 \
865 cpu_notifier_register_begin(); \
e7e7ee2e 866 fn(&fn##_nb, (unsigned long)CPU_UP_PREPARE, \
c13d38e4 867 (void *)(unsigned long)cpu); \
6760bca9 868 local_irq_save(flags); \
e7e7ee2e 869 fn(&fn##_nb, (unsigned long)CPU_STARTING, \
c13d38e4 870 (void *)(unsigned long)cpu); \
6760bca9 871 local_irq_restore(flags); \
e7e7ee2e 872 fn(&fn##_nb, (unsigned long)CPU_ONLINE, \
c13d38e4 873 (void *)(unsigned long)cpu); \
f0bdb5e0
SB
874 __register_cpu_notifier(&fn##_nb); \
875 cpu_notifier_register_done(); \
3f6da390
PZ
876} while (0)
877
f0bdb5e0
SB
878/*
879 * Bare-bones version of perf_cpu_notifier(), which doesn't invoke the
880 * callback for already online CPUs.
881 */
882#define __perf_cpu_notifier(fn) \
883do { \
884 static struct notifier_block fn##_nb = \
885 { .notifier_call = fn, .priority = CPU_PRI_PERF }; \
886 \
887 __register_cpu_notifier(&fn##_nb); \
888} while (0)
641cc938 889
2663960c
SB
890struct perf_pmu_events_attr {
891 struct device_attribute attr;
892 u64 id;
3a54aaa0 893 const char *event_str;
2663960c
SB
894};
895
896#define PMU_EVENT_ATTR(_name, _var, _id, _show) \
897static struct perf_pmu_events_attr _var = { \
898 .attr = __ATTR(_name, 0444, _show, NULL), \
899 .id = _id, \
900};
901
641cc938
JO
902#define PMU_FORMAT_ATTR(_name, _format) \
903static ssize_t \
904_name##_show(struct device *dev, \
905 struct device_attribute *attr, \
906 char *page) \
907{ \
908 BUILD_BUG_ON(sizeof(_format) >= PAGE_SIZE); \
909 return sprintf(page, _format "\n"); \
910} \
911 \
912static struct device_attribute format_attr_##_name = __ATTR_RO(_name)
913
cdd6c482 914#endif /* _LINUX_PERF_EVENT_H */
This page took 0.410283 seconds and 5 git commands to generate.