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