]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - include/linux/perf_event.h
perf/x86/intel/pt: Fix KVM warning due to doing rdmsr() before the CPUID test
[mirror_ubuntu-bionic-kernel.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>
39bed6cb 56#include <linux/cgroup.h>
fa588151 57#include <asm/local.h>
f3dfd265 58
f9188e02
PZ
59struct perf_callchain_entry {
60 __u64 nr;
61 __u64 ip[PERF_MAX_STACK_DEPTH];
62};
63
3a43ce68
FW
64struct perf_raw_record {
65 u32 size;
66 void *data;
f413cdb8
FW
67};
68
bce38cd5
SE
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 */
caff2bef
PZ
78struct perf_branch_stack {
79 __u64 nr;
80 struct perf_branch_entry entries[0];
81};
82
f3dfd265
PM
83struct task_struct;
84
efc9f05d
SE
85/*
86 * extra PMU register associated with an event
87 */
88struct 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
0793a61d 95/**
cdd6c482 96 * struct hw_perf_event - performance event hardware details:
0793a61d 97 */
cdd6c482
IM
98struct hw_perf_event {
99#ifdef CONFIG_PERF_EVENTS
d6d020e9
PZ
100 union {
101 struct { /* hardware */
a308444c 102 u64 config;
447a194b 103 u64 last_tag;
a308444c 104 unsigned long config_base;
cdd6c482 105 unsigned long event_base;
c48b6053 106 int event_base_rdpmc;
a308444c 107 int idx;
447a194b 108 int last_cpu;
9fac2cf3 109 int flags;
bce38cd5 110
efc9f05d 111 struct hw_perf_event_extra extra_reg;
bce38cd5 112 struct hw_perf_event_extra branch_reg;
d6d020e9 113 };
721a669b 114 struct { /* software */
a308444c 115 struct hrtimer hrtimer;
d6d020e9 116 };
f22c1bb6 117 struct { /* tracepoint */
f22c1bb6
ON
118 /* for tp_event->class */
119 struct list_head tp_list;
120 };
4afbb24c
MF
121 struct { /* intel_cqm */
122 int cqm_state;
b3df4ec4 123 u32 cqm_rmid;
4afbb24c
MF
124 struct list_head cqm_events_entry;
125 struct list_head cqm_groups_entry;
126 struct list_head cqm_group_entry;
127 };
ec0d7729
AS
128 struct { /* itrace */
129 int itrace_started;
130 };
24f1e32c 131#ifdef CONFIG_HAVE_HW_BREAKPOINT
45a73372 132 struct { /* breakpoint */
d580ff86
PZ
133 /*
134 * Crufty hack to avoid the chicken and egg
135 * problem hw_breakpoint has with context
136 * creation and event initalization.
137 */
f22c1bb6
ON
138 struct arch_hw_breakpoint info;
139 struct list_head bp_list;
45a73372 140 };
24f1e32c 141#endif
d6d020e9 142 };
50f16a8b 143 struct task_struct *target;
a4eaf7f1 144 int state;
e7850595 145 local64_t prev_count;
b23f3325 146 u64 sample_period;
9e350de3 147 u64 last_period;
e7850595 148 local64_t period_left;
e050e3f0 149 u64 interrupts_seq;
60db5e09 150 u64 interrupts;
6a24ed6c 151
abd50713
PZ
152 u64 freq_time_stamp;
153 u64 freq_count_stamp;
ee06094f 154#endif
0793a61d
TG
155};
156
a4eaf7f1
PZ
157/*
158 * hw_perf_event::state flags
159 */
160#define PERF_HES_STOPPED 0x01 /* the counter is stopped */
161#define PERF_HES_UPTODATE 0x02 /* event->count up-to-date */
162#define PERF_HES_ARCH 0x04
163
cdd6c482 164struct perf_event;
621a01ea 165
8d2cacbb
PZ
166/*
167 * Common implementation detail of pmu::{start,commit,cancel}_txn
168 */
169#define PERF_EVENT_TXN 0x1
6bde9b6c 170
53b25335
VW
171/**
172 * pmu::capabilities flags
173 */
174#define PERF_PMU_CAP_NO_INTERRUPT 0x01
34f43927 175#define PERF_PMU_CAP_NO_NMI 0x02
0a4e38e6 176#define PERF_PMU_CAP_AUX_NO_SG 0x04
6a279230 177#define PERF_PMU_CAP_AUX_SW_DOUBLEBUF 0x08
bed5b25a 178#define PERF_PMU_CAP_EXCLUSIVE 0x10
ec0d7729 179#define PERF_PMU_CAP_ITRACE 0x20
53b25335 180
621a01ea 181/**
4aeb0b42 182 * struct pmu - generic performance monitoring unit
621a01ea 183 */
4aeb0b42 184struct pmu {
b0a873eb
PZ
185 struct list_head entry;
186
c464c76e 187 struct module *module;
abe43400 188 struct device *dev;
0c9d42ed 189 const struct attribute_group **attr_groups;
03d8e80b 190 const char *name;
2e80a82a
PZ
191 int type;
192
53b25335
VW
193 /*
194 * various common per-pmu feature flags
195 */
196 int capabilities;
197
108b02cf
PZ
198 int * __percpu pmu_disable_count;
199 struct perf_cpu_context * __percpu pmu_cpu_context;
bed5b25a 200 atomic_t exclusive_cnt; /* < 0: cpu; > 0: tsk */
8dc85d54 201 int task_ctx_nr;
62b85639 202 int hrtimer_interval_ms;
6bde9b6c
LM
203
204 /*
a4eaf7f1
PZ
205 * Fully disable/enable this PMU, can be used to protect from the PMI
206 * as well as for lazy/batch writing of the MSRs.
6bde9b6c 207 */
ad5133b7
PZ
208 void (*pmu_enable) (struct pmu *pmu); /* optional */
209 void (*pmu_disable) (struct pmu *pmu); /* optional */
6bde9b6c 210
8d2cacbb 211 /*
a4eaf7f1 212 * Try and initialize the event for this PMU.
24cd7f54 213 * Should return -ENOENT when the @event doesn't match this PMU.
8d2cacbb 214 */
b0a873eb
PZ
215 int (*event_init) (struct perf_event *event);
216
1e0fb9ec
AL
217 /*
218 * Notification that the event was mapped or unmapped. Called
219 * in the context of the mapping task.
220 */
221 void (*event_mapped) (struct perf_event *event); /*optional*/
222 void (*event_unmapped) (struct perf_event *event); /*optional*/
223
a4eaf7f1
PZ
224#define PERF_EF_START 0x01 /* start the counter when adding */
225#define PERF_EF_RELOAD 0x02 /* reload the counter when starting */
226#define PERF_EF_UPDATE 0x04 /* update the counter when stopping */
227
8d2cacbb 228 /*
a4eaf7f1
PZ
229 * Adds/Removes a counter to/from the PMU, can be done inside
230 * a transaction, see the ->*_txn() methods.
231 */
232 int (*add) (struct perf_event *event, int flags);
233 void (*del) (struct perf_event *event, int flags);
234
235 /*
236 * Starts/Stops a counter present on the PMU. The PMI handler
237 * should stop the counter when perf_event_overflow() returns
238 * !0. ->start() will be used to continue.
239 */
240 void (*start) (struct perf_event *event, int flags);
241 void (*stop) (struct perf_event *event, int flags);
242
243 /*
244 * Updates the counter value of the event.
245 */
cdd6c482 246 void (*read) (struct perf_event *event);
6bde9b6c
LM
247
248 /*
24cd7f54
PZ
249 * Group events scheduling is treated as a transaction, add
250 * group events as a whole and perform one schedulability test.
251 * If the test fails, roll back the whole group
a4eaf7f1
PZ
252 *
253 * Start the transaction, after this ->add() doesn't need to
24cd7f54 254 * do schedulability tests.
8d2cacbb 255 */
e7e7ee2e 256 void (*start_txn) (struct pmu *pmu); /* optional */
8d2cacbb 257 /*
a4eaf7f1 258 * If ->start_txn() disabled the ->add() schedulability test
8d2cacbb
PZ
259 * then ->commit_txn() is required to perform one. On success
260 * the transaction is closed. On error the transaction is kept
261 * open until ->cancel_txn() is called.
262 */
e7e7ee2e 263 int (*commit_txn) (struct pmu *pmu); /* optional */
8d2cacbb 264 /*
a4eaf7f1 265 * Will cancel the transaction, assumes ->del() is called
25985edc 266 * for each successful ->add() during the transaction.
8d2cacbb 267 */
e7e7ee2e 268 void (*cancel_txn) (struct pmu *pmu); /* optional */
35edc2a5
PZ
269
270 /*
271 * Will return the value for perf_event_mmap_page::index for this event,
272 * if no implementation is provided it will default to: event->hw.idx + 1.
273 */
274 int (*event_idx) (struct perf_event *event); /*optional */
d010b332 275
ba532500
YZ
276 /*
277 * context-switches callback
278 */
279 void (*sched_task) (struct perf_event_context *ctx,
280 bool sched_in);
4af57ef2
YZ
281 /*
282 * PMU specific data size
283 */
284 size_t task_ctx_size;
ba532500 285
eacd3ecc
MF
286
287 /*
288 * Return the count value for a counter.
289 */
290 u64 (*count) (struct perf_event *event); /*optional*/
45bfb2e5
PZ
291
292 /*
293 * Set up pmu-private data structures for an AUX area
294 */
295 void *(*setup_aux) (int cpu, void **pages,
296 int nr_pages, bool overwrite);
297 /* optional */
298
299 /*
300 * Free pmu-private AUX data structures
301 */
302 void (*free_aux) (void *aux); /* optional */
66eb579e
MR
303
304 /*
305 * Filter events for PMU-specific reasons.
306 */
307 int (*filter_match) (struct perf_event *event); /* optional */
621a01ea
IM
308};
309
6a930700 310/**
cdd6c482 311 * enum perf_event_active_state - the states of a event
6a930700 312 */
cdd6c482 313enum perf_event_active_state {
179033b3 314 PERF_EVENT_STATE_EXIT = -3,
57c0c15b 315 PERF_EVENT_STATE_ERROR = -2,
cdd6c482
IM
316 PERF_EVENT_STATE_OFF = -1,
317 PERF_EVENT_STATE_INACTIVE = 0,
57c0c15b 318 PERF_EVENT_STATE_ACTIVE = 1,
6a930700
IM
319};
320
9b51f66d 321struct file;
453f19ee
PZ
322struct perf_sample_data;
323
a8b0ca17 324typedef void (*perf_overflow_handler_t)(struct perf_event *,
b326e956
FW
325 struct perf_sample_data *,
326 struct pt_regs *regs);
327
d6f962b5 328enum perf_group_flag {
e7e7ee2e 329 PERF_GROUP_SOFTWARE = 0x1,
d6f962b5
FW
330};
331
e7e7ee2e
IM
332#define SWEVENT_HLIST_BITS 8
333#define SWEVENT_HLIST_SIZE (1 << SWEVENT_HLIST_BITS)
76e1d904
FW
334
335struct swevent_hlist {
e7e7ee2e
IM
336 struct hlist_head heads[SWEVENT_HLIST_SIZE];
337 struct rcu_head rcu_head;
76e1d904
FW
338};
339
8a49542c
PZ
340#define PERF_ATTACH_CONTEXT 0x01
341#define PERF_ATTACH_GROUP 0x02
d580ff86 342#define PERF_ATTACH_TASK 0x04
4af57ef2 343#define PERF_ATTACH_TASK_DATA 0x08
8a49542c 344
877c6856 345struct perf_cgroup;
76369139
FW
346struct ring_buffer;
347
0793a61d 348/**
cdd6c482 349 * struct perf_event - performance event kernel representation:
0793a61d 350 */
cdd6c482
IM
351struct perf_event {
352#ifdef CONFIG_PERF_EVENTS
9886167d
PZ
353 /*
354 * entry onto perf_event_context::event_list;
355 * modifications require ctx->lock
356 * RCU safe iterations.
357 */
592903cd 358 struct list_head event_entry;
9886167d
PZ
359
360 /*
361 * XXX: group_entry and sibling_list should be mutually exclusive;
362 * either you're a sibling on a group, or you're the group leader.
363 * Rework the code to always use the same list element.
364 *
365 * Locked for modification by both ctx->mutex and ctx->lock; holding
366 * either sufficies for read.
367 */
368 struct list_head group_entry;
04289bb9 369 struct list_head sibling_list;
9886167d
PZ
370
371 /*
372 * We need storage to track the entries in perf_pmu_migrate_context; we
373 * cannot use the event_entry because of RCU and we want to keep the
374 * group in tact which avoids us using the other two entries.
375 */
376 struct list_head migrate_entry;
377
f3ae75de
SE
378 struct hlist_node hlist_entry;
379 struct list_head active_entry;
0127c3ea 380 int nr_siblings;
d6f962b5 381 int group_flags;
cdd6c482 382 struct perf_event *group_leader;
a4eaf7f1 383 struct pmu *pmu;
04289bb9 384
cdd6c482 385 enum perf_event_active_state state;
8a49542c 386 unsigned int attach_state;
e7850595 387 local64_t count;
a6e6dea6 388 atomic64_t child_count;
ee06094f 389
53cfbf59 390 /*
cdd6c482 391 * These are the total time in nanoseconds that the event
53cfbf59 392 * has been enabled (i.e. eligible to run, and the task has
cdd6c482 393 * been scheduled in, if this is a per-task event)
53cfbf59
PM
394 * and running (scheduled onto the CPU), respectively.
395 *
396 * They are computed from tstamp_enabled, tstamp_running and
cdd6c482 397 * tstamp_stopped when the event is in INACTIVE or ACTIVE state.
53cfbf59
PM
398 */
399 u64 total_time_enabled;
400 u64 total_time_running;
401
402 /*
403 * These are timestamps used for computing total_time_enabled
cdd6c482 404 * and total_time_running when the event is in INACTIVE or
53cfbf59
PM
405 * ACTIVE state, measured in nanoseconds from an arbitrary point
406 * in time.
cdd6c482
IM
407 * tstamp_enabled: the notional time when the event was enabled
408 * tstamp_running: the notional time when the event was scheduled on
53cfbf59 409 * tstamp_stopped: in INACTIVE state, the notional time when the
cdd6c482 410 * event was scheduled off.
53cfbf59
PM
411 */
412 u64 tstamp_enabled;
413 u64 tstamp_running;
414 u64 tstamp_stopped;
415
eed01528
SE
416 /*
417 * timestamp shadows the actual context timing but it can
418 * be safely used in NMI interrupt context. It reflects the
419 * context time as it was when the event was last scheduled in.
420 *
421 * ctx_time already accounts for ctx->timestamp. Therefore to
422 * compute ctx_time for a sample, simply add perf_clock().
423 */
424 u64 shadow_ctx_time;
425
24f1e32c 426 struct perf_event_attr attr;
c320c7b7 427 u16 header_size;
6844c09d 428 u16 id_header_size;
c320c7b7 429 u16 read_size;
cdd6c482 430 struct hw_perf_event hw;
0793a61d 431
cdd6c482 432 struct perf_event_context *ctx;
a6fa941d 433 atomic_long_t refcount;
0793a61d 434
53cfbf59
PM
435 /*
436 * These accumulate total time (in nanoseconds) that children
cdd6c482 437 * events have been enabled and running, respectively.
53cfbf59
PM
438 */
439 atomic64_t child_total_time_enabled;
440 atomic64_t child_total_time_running;
441
0793a61d 442 /*
d859e29f 443 * Protect attach/detach and child_list:
0793a61d 444 */
fccc714b
PZ
445 struct mutex child_mutex;
446 struct list_head child_list;
cdd6c482 447 struct perf_event *parent;
0793a61d
TG
448
449 int oncpu;
450 int cpu;
451
082ff5a2
PZ
452 struct list_head owner_entry;
453 struct task_struct *owner;
454
7b732a75
PZ
455 /* mmap bits */
456 struct mutex mmap_mutex;
457 atomic_t mmap_count;
26cb63ad 458
76369139 459 struct ring_buffer *rb;
10c6db11 460 struct list_head rb_entry;
b69cf536
PZ
461 unsigned long rcu_batches;
462 int rcu_pending;
37d81828 463
7b732a75 464 /* poll related */
0793a61d 465 wait_queue_head_t waitq;
3c446b3d 466 struct fasync_struct *fasync;
79f14641
PZ
467
468 /* delayed work for NMIs and such */
469 int pending_wakeup;
4c9e2542 470 int pending_kill;
79f14641 471 int pending_disable;
e360adbe 472 struct irq_work pending;
592903cd 473
79f14641
PZ
474 atomic_t event_limit;
475
cdd6c482 476 void (*destroy)(struct perf_event *);
592903cd 477 struct rcu_head rcu_head;
709e50cf
PZ
478
479 struct pid_namespace *ns;
8e5799b1 480 u64 id;
6fb2915d 481
34f43927 482 u64 (*clock)(void);
b326e956 483 perf_overflow_handler_t overflow_handler;
4dc0da86 484 void *overflow_handler_context;
453f19ee 485
07b139c8 486#ifdef CONFIG_EVENT_TRACING
2425bcb9 487 struct trace_event_call *tp_event;
6fb2915d 488 struct event_filter *filter;
ced39002
JO
489#ifdef CONFIG_FUNCTION_TRACER
490 struct ftrace_ops ftrace_ops;
491#endif
ee06094f 492#endif
6fb2915d 493
e5d1367f
SE
494#ifdef CONFIG_CGROUP_PERF
495 struct perf_cgroup *cgrp; /* cgroup event is attach to */
496 int cgrp_defer_enabled;
497#endif
498
6fb2915d 499#endif /* CONFIG_PERF_EVENTS */
0793a61d
TG
500};
501
502/**
cdd6c482 503 * struct perf_event_context - event context structure
0793a61d 504 *
cdd6c482 505 * Used as a container for task events and CPU events as well:
0793a61d 506 */
cdd6c482 507struct perf_event_context {
108b02cf 508 struct pmu *pmu;
0793a61d 509 /*
cdd6c482 510 * Protect the states of the events in the list,
d859e29f 511 * nr_active, and the list:
0793a61d 512 */
e625cce1 513 raw_spinlock_t lock;
d859e29f 514 /*
cdd6c482 515 * Protect the list of events. Locking either mutex or lock
d859e29f
PM
516 * is sufficient to ensure the list doesn't change; to change
517 * the list you need to lock both the mutex and the spinlock.
518 */
a308444c 519 struct mutex mutex;
04289bb9 520
2fde4f94 521 struct list_head active_ctx_list;
889ff015
FW
522 struct list_head pinned_groups;
523 struct list_head flexible_groups;
a308444c 524 struct list_head event_list;
cdd6c482 525 int nr_events;
a308444c
IM
526 int nr_active;
527 int is_active;
bfbd3381 528 int nr_stat;
0f5a2601 529 int nr_freq;
dddd3379 530 int rotate_disable;
a308444c
IM
531 atomic_t refcount;
532 struct task_struct *task;
53cfbf59
PM
533
534 /*
4af4998b 535 * Context clock, runs when context enabled.
53cfbf59 536 */
a308444c
IM
537 u64 time;
538 u64 timestamp;
564c2b21
PM
539
540 /*
541 * These fields let us detect when two contexts have both
542 * been cloned (inherited) from a common ancestor.
543 */
cdd6c482 544 struct perf_event_context *parent_ctx;
a308444c
IM
545 u64 parent_gen;
546 u64 generation;
547 int pin_count;
d010b332 548 int nr_cgroups; /* cgroup evts */
4af57ef2 549 void *task_ctx_data; /* pmu specific data */
28009ce4 550 struct rcu_head rcu_head;
fadfe7be
JO
551
552 struct delayed_work orphans_remove;
553 bool orphans_remove_sched;
0793a61d
TG
554};
555
7ae07ea3
FW
556/*
557 * Number of contexts where an event can trigger:
e7e7ee2e 558 * task, softirq, hardirq, nmi.
7ae07ea3
FW
559 */
560#define PERF_NR_CONTEXTS 4
561
0793a61d 562/**
cdd6c482 563 * struct perf_event_cpu_context - per cpu event context structure
0793a61d
TG
564 */
565struct perf_cpu_context {
cdd6c482
IM
566 struct perf_event_context ctx;
567 struct perf_event_context *task_ctx;
0793a61d 568 int active_oncpu;
3b6f9e5c 569 int exclusive;
4cfafd30
PZ
570
571 raw_spinlock_t hrtimer_lock;
9e630205
SE
572 struct hrtimer hrtimer;
573 ktime_t hrtimer_interval;
4cfafd30
PZ
574 unsigned int hrtimer_active;
575
3f1f3320 576 struct pmu *unique_pmu;
e5d1367f 577 struct perf_cgroup *cgrp;
0793a61d
TG
578};
579
5622f295 580struct perf_output_handle {
57c0c15b 581 struct perf_event *event;
76369139 582 struct ring_buffer *rb;
6d1acfd5 583 unsigned long wakeup;
5d967a8b 584 unsigned long size;
fdc26706
AS
585 union {
586 void *addr;
587 unsigned long head;
588 };
5d967a8b 589 int page;
5622f295
MM
590};
591
39bed6cb
MF
592#ifdef CONFIG_CGROUP_PERF
593
594/*
595 * perf_cgroup_info keeps track of time_enabled for a cgroup.
596 * This is a per-cpu dynamically allocated data structure.
597 */
598struct perf_cgroup_info {
599 u64 time;
600 u64 timestamp;
601};
602
603struct perf_cgroup {
604 struct cgroup_subsys_state css;
605 struct perf_cgroup_info __percpu *info;
606};
607
608/*
609 * Must ensure cgroup is pinned (css_get) before calling
610 * this function. In other words, we cannot call this function
611 * if there is no cgroup event for the current CPU context.
612 */
613static inline struct perf_cgroup *
614perf_cgroup_from_task(struct task_struct *task)
615{
616 return container_of(task_css(task, perf_event_cgrp_id),
617 struct perf_cgroup, css);
618}
619#endif /* CONFIG_CGROUP_PERF */
620
cdd6c482 621#ifdef CONFIG_PERF_EVENTS
829b42dd 622
fdc26706
AS
623extern void *perf_aux_output_begin(struct perf_output_handle *handle,
624 struct perf_event *event);
625extern void perf_aux_output_end(struct perf_output_handle *handle,
626 unsigned long size, bool truncated);
627extern int perf_aux_output_skip(struct perf_output_handle *handle,
628 unsigned long size);
629extern void *perf_get_aux(struct perf_output_handle *handle);
630
03d8e80b 631extern int perf_pmu_register(struct pmu *pmu, const char *name, int type);
b0a873eb 632extern void perf_pmu_unregister(struct pmu *pmu);
621a01ea 633
3bf101ba 634extern int perf_num_counters(void);
84c79910 635extern const char *perf_pmu_name(void);
ab0cce56
JO
636extern void __perf_event_task_sched_in(struct task_struct *prev,
637 struct task_struct *task);
638extern void __perf_event_task_sched_out(struct task_struct *prev,
639 struct task_struct *next);
cdd6c482
IM
640extern int perf_event_init_task(struct task_struct *child);
641extern void perf_event_exit_task(struct task_struct *child);
642extern void perf_event_free_task(struct task_struct *task);
4e231c79 643extern void perf_event_delayed_put(struct task_struct *task);
ffe8690c
KX
644extern struct perf_event *perf_event_get(unsigned int fd);
645extern const struct perf_event_attr *perf_event_attrs(struct perf_event *event);
cdd6c482 646extern void perf_event_print_debug(void);
33696fc0
PZ
647extern void perf_pmu_disable(struct pmu *pmu);
648extern void perf_pmu_enable(struct pmu *pmu);
ba532500
YZ
649extern void perf_sched_cb_dec(struct pmu *pmu);
650extern void perf_sched_cb_inc(struct pmu *pmu);
cdd6c482
IM
651extern int perf_event_task_disable(void);
652extern int perf_event_task_enable(void);
26ca5c11 653extern int perf_event_refresh(struct perf_event *event, int refresh);
cdd6c482 654extern void perf_event_update_userpage(struct perf_event *event);
fb0459d7
AV
655extern int perf_event_release_kernel(struct perf_event *event);
656extern struct perf_event *
657perf_event_create_kernel_counter(struct perf_event_attr *attr,
658 int cpu,
38a81da2 659 struct task_struct *task,
4dc0da86
AK
660 perf_overflow_handler_t callback,
661 void *context);
0cda4c02
YZ
662extern void perf_pmu_migrate_context(struct pmu *pmu,
663 int src_cpu, int dst_cpu);
ffe8690c 664extern u64 perf_event_read_local(struct perf_event *event);
59ed446f
PZ
665extern u64 perf_event_read_value(struct perf_event *event,
666 u64 *enabled, u64 *running);
5c92d124 667
d010b332 668
df1a132b 669struct perf_sample_data {
2565711f
PZ
670 /*
671 * Fields set by perf_sample_data_init(), group so as to
672 * minimize the cachelines touched.
673 */
674 u64 addr;
675 struct perf_raw_record *raw;
676 struct perf_branch_stack *br_stack;
677 u64 period;
678 u64 weight;
679 u64 txn;
680 union perf_mem_data_src data_src;
5622f295 681
2565711f
PZ
682 /*
683 * The other fields, optionally {set,used} by
684 * perf_{prepare,output}_sample().
685 */
686 u64 type;
5622f295
MM
687 u64 ip;
688 struct {
689 u32 pid;
690 u32 tid;
691 } tid_entry;
692 u64 time;
5622f295
MM
693 u64 id;
694 u64 stream_id;
695 struct {
696 u32 cpu;
697 u32 reserved;
698 } cpu_entry;
5622f295 699 struct perf_callchain_entry *callchain;
88a7c26a
AL
700
701 /*
702 * regs_user may point to task_pt_regs or to regs_user_copy, depending
703 * on arch details.
704 */
60e2364e 705 struct perf_regs regs_user;
88a7c26a
AL
706 struct pt_regs regs_user_copy;
707
60e2364e 708 struct perf_regs regs_intr;
c5ebcedb 709 u64 stack_user_size;
2565711f 710} ____cacheline_aligned;
df1a132b 711
770eee1f
SE
712/* default value for data source */
713#define PERF_MEM_NA (PERF_MEM_S(OP, NA) |\
714 PERF_MEM_S(LVL, NA) |\
715 PERF_MEM_S(SNOOP, NA) |\
716 PERF_MEM_S(LOCK, NA) |\
717 PERF_MEM_S(TLB, NA))
718
fd0d000b
RR
719static inline void perf_sample_data_init(struct perf_sample_data *data,
720 u64 addr, u64 period)
dc1d628a 721{
fd0d000b 722 /* remaining struct members initialized in perf_prepare_sample() */
dc1d628a
PZ
723 data->addr = addr;
724 data->raw = NULL;
bce38cd5 725 data->br_stack = NULL;
4018994f 726 data->period = period;
c3feedf2 727 data->weight = 0;
770eee1f 728 data->data_src.val = PERF_MEM_NA;
fdfbbd07 729 data->txn = 0;
dc1d628a
PZ
730}
731
5622f295
MM
732extern void perf_output_sample(struct perf_output_handle *handle,
733 struct perf_event_header *header,
734 struct perf_sample_data *data,
cdd6c482 735 struct perf_event *event);
5622f295
MM
736extern void perf_prepare_sample(struct perf_event_header *header,
737 struct perf_sample_data *data,
cdd6c482 738 struct perf_event *event,
5622f295
MM
739 struct pt_regs *regs);
740
a8b0ca17 741extern int perf_event_overflow(struct perf_event *event,
5622f295
MM
742 struct perf_sample_data *data,
743 struct pt_regs *regs);
df1a132b 744
21509084
YZ
745extern void perf_event_output(struct perf_event *event,
746 struct perf_sample_data *data,
747 struct pt_regs *regs);
748
749extern void
750perf_event_header__init_id(struct perf_event_header *header,
751 struct perf_sample_data *data,
752 struct perf_event *event);
753extern void
754perf_event__output_id_sample(struct perf_event *event,
755 struct perf_output_handle *handle,
756 struct perf_sample_data *sample);
757
f38b0dbb
KL
758extern void
759perf_log_lost_samples(struct perf_event *event, u64 lost);
760
6c7e550f
FBH
761static inline bool is_sampling_event(struct perf_event *event)
762{
763 return event->attr.sample_period != 0;
764}
765
3b6f9e5c 766/*
cdd6c482 767 * Return 1 for a software event, 0 for a hardware event
3b6f9e5c 768 */
cdd6c482 769static inline int is_software_event(struct perf_event *event)
3b6f9e5c 770{
89a1e187 771 return event->pmu->task_ctx_nr == perf_sw_context;
3b6f9e5c
PM
772}
773
c5905afb 774extern struct static_key perf_swevent_enabled[PERF_COUNT_SW_MAX];
f29ac756 775
86038c5e 776extern void ___perf_sw_event(u32, u64, struct pt_regs *, u64);
a8b0ca17 777extern void __perf_sw_event(u32, u64, struct pt_regs *, u64);
f29ac756 778
b0f82b81 779#ifndef perf_arch_fetch_caller_regs
e7e7ee2e 780static inline void perf_arch_fetch_caller_regs(struct pt_regs *regs, unsigned long ip) { }
b0f82b81 781#endif
5331d7b8
FW
782
783/*
784 * Take a snapshot of the regs. Skip ip and frame pointer to
785 * the nth caller. We only need a few of the regs:
786 * - ip for PERF_SAMPLE_IP
787 * - cs for user_mode() tests
788 * - bp for callchains
789 * - eflags, for future purposes, just in case
790 */
b0f82b81 791static inline void perf_fetch_caller_regs(struct pt_regs *regs)
5331d7b8 792{
5331d7b8
FW
793 memset(regs, 0, sizeof(*regs));
794
b0f82b81 795 perf_arch_fetch_caller_regs(regs, CALLER_ADDR0);
5331d7b8
FW
796}
797
7e54a5a0 798static __always_inline void
a8b0ca17 799perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr)
e49a5bd3 800{
86038c5e
PZI
801 if (static_key_false(&perf_swevent_enabled[event_id]))
802 __perf_sw_event(event_id, nr, regs, addr);
803}
804
805DECLARE_PER_CPU(struct pt_regs, __perf_regs[4]);
7e54a5a0 806
86038c5e
PZI
807/*
808 * 'Special' version for the scheduler, it hard assumes no recursion,
809 * which is guaranteed by us not actually scheduling inside other swevents
810 * because those disable preemption.
811 */
812static __always_inline void
813perf_sw_event_sched(u32 event_id, u64 nr, u64 addr)
814{
c5905afb 815 if (static_key_false(&perf_swevent_enabled[event_id])) {
86038c5e
PZI
816 struct pt_regs *regs = this_cpu_ptr(&__perf_regs[0]);
817
818 perf_fetch_caller_regs(regs);
819 ___perf_sw_event(event_id, nr, regs, addr);
e49a5bd3
FW
820 }
821}
822
c5905afb 823extern struct static_key_deferred perf_sched_events;
ee6dcfa4 824
ff303e66
PZ
825static __always_inline bool
826perf_sw_migrate_enabled(void)
827{
828 if (static_key_false(&perf_swevent_enabled[PERF_COUNT_SW_CPU_MIGRATIONS]))
829 return true;
830 return false;
831}
832
833static inline void perf_event_task_migrate(struct task_struct *task)
834{
835 if (perf_sw_migrate_enabled())
836 task->sched_migrated = 1;
837}
838
ab0cce56 839static inline void perf_event_task_sched_in(struct task_struct *prev,
a8d757ef 840 struct task_struct *task)
ab0cce56
JO
841{
842 if (static_key_false(&perf_sched_events.key))
843 __perf_event_task_sched_in(prev, task);
ff303e66
PZ
844
845 if (perf_sw_migrate_enabled() && task->sched_migrated) {
846 struct pt_regs *regs = this_cpu_ptr(&__perf_regs[0]);
847
848 perf_fetch_caller_regs(regs);
849 ___perf_sw_event(PERF_COUNT_SW_CPU_MIGRATIONS, 1, regs, 0);
850 task->sched_migrated = 0;
851 }
ab0cce56
JO
852}
853
854static inline void perf_event_task_sched_out(struct task_struct *prev,
855 struct task_struct *next)
ee6dcfa4 856{
86038c5e 857 perf_sw_event_sched(PERF_COUNT_SW_CONTEXT_SWITCHES, 1, 0);
ee6dcfa4 858
c5905afb 859 if (static_key_false(&perf_sched_events.key))
ab0cce56 860 __perf_event_task_sched_out(prev, next);
ee6dcfa4
PZ
861}
862
eacd3ecc
MF
863static inline u64 __perf_event_count(struct perf_event *event)
864{
865 return local64_read(&event->count) + atomic64_read(&event->child_count);
866}
867
3af9e859 868extern void perf_event_mmap(struct vm_area_struct *vma);
39447b38 869extern struct perf_guest_info_callbacks *perf_guest_cbs;
dcf46b94
ZY
870extern int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *callbacks);
871extern int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *callbacks);
39447b38 872
e041e328 873extern void perf_event_exec(void);
82b89778 874extern void perf_event_comm(struct task_struct *tsk, bool exec);
cdd6c482 875extern void perf_event_fork(struct task_struct *tsk);
8d1b2d93 876
56962b44
FW
877/* Callchains */
878DECLARE_PER_CPU(struct perf_callchain_entry, perf_callchain_entry);
879
e7e7ee2e
IM
880extern void perf_callchain_user(struct perf_callchain_entry *entry, struct pt_regs *regs);
881extern void perf_callchain_kernel(struct perf_callchain_entry *entry, struct pt_regs *regs);
394ee076 882
e7e7ee2e 883static inline void perf_callchain_store(struct perf_callchain_entry *entry, u64 ip)
70791ce9
FW
884{
885 if (entry->nr < PERF_MAX_STACK_DEPTH)
886 entry->ip[entry->nr++] = ip;
887}
394ee076 888
cdd6c482
IM
889extern int sysctl_perf_event_paranoid;
890extern int sysctl_perf_event_mlock;
891extern int sysctl_perf_event_sample_rate;
14c63f17
DH
892extern int sysctl_perf_cpu_time_max_percent;
893
894extern void perf_sample_event_took(u64 sample_len_ns);
1ccd1549 895
163ec435
PZ
896extern int perf_proc_update_handler(struct ctl_table *table, int write,
897 void __user *buffer, size_t *lenp,
898 loff_t *ppos);
14c63f17
DH
899extern int perf_cpu_time_max_percent_handler(struct ctl_table *table, int write,
900 void __user *buffer, size_t *lenp,
901 loff_t *ppos);
902
163ec435 903
320ebf09
PZ
904static inline bool perf_paranoid_tracepoint_raw(void)
905{
906 return sysctl_perf_event_paranoid > -1;
907}
908
909static inline bool perf_paranoid_cpu(void)
910{
911 return sysctl_perf_event_paranoid > 0;
912}
913
914static inline bool perf_paranoid_kernel(void)
915{
916 return sysctl_perf_event_paranoid > 1;
917}
918
cdd6c482 919extern void perf_event_init(void);
1c024eca
PZ
920extern void perf_tp_event(u64 addr, u64 count, void *record,
921 int entry_size, struct pt_regs *regs,
e6dab5ff
AV
922 struct hlist_head *head, int rctx,
923 struct task_struct *task);
24f1e32c 924extern void perf_bp_event(struct perf_event *event, void *data);
0d905bca 925
9d23a90a 926#ifndef perf_misc_flags
e7e7ee2e
IM
927# define perf_misc_flags(regs) \
928 (user_mode(regs) ? PERF_RECORD_MISC_USER : PERF_RECORD_MISC_KERNEL)
929# define perf_instruction_pointer(regs) instruction_pointer(regs)
9d23a90a
PM
930#endif
931
bce38cd5
SE
932static inline bool has_branch_stack(struct perf_event *event)
933{
934 return event->attr.sample_type & PERF_SAMPLE_BRANCH_STACK;
a46a2300
YZ
935}
936
937static inline bool needs_branch_stack(struct perf_event *event)
938{
939 return event->attr.branch_sample_type != 0;
bce38cd5
SE
940}
941
45bfb2e5
PZ
942static inline bool has_aux(struct perf_event *event)
943{
944 return event->pmu->setup_aux;
945}
946
5622f295 947extern int perf_output_begin(struct perf_output_handle *handle,
a7ac67ea 948 struct perf_event *event, unsigned int size);
5622f295 949extern void perf_output_end(struct perf_output_handle *handle);
91d7753a 950extern unsigned int perf_output_copy(struct perf_output_handle *handle,
5622f295 951 const void *buf, unsigned int len);
5685e0ff
JO
952extern unsigned int perf_output_skip(struct perf_output_handle *handle,
953 unsigned int len);
4ed7c92d
PZ
954extern int perf_swevent_get_recursion_context(void);
955extern void perf_swevent_put_recursion_context(int rctx);
ab573844 956extern u64 perf_swevent_set_period(struct perf_event *event);
44234adc
FW
957extern void perf_event_enable(struct perf_event *event);
958extern void perf_event_disable(struct perf_event *event);
500ad2d8 959extern int __perf_event_disable(void *info);
e9d2b064 960extern void perf_event_task_tick(void);
e041e328 961#else /* !CONFIG_PERF_EVENTS: */
fdc26706
AS
962static inline void *
963perf_aux_output_begin(struct perf_output_handle *handle,
964 struct perf_event *event) { return NULL; }
965static inline void
966perf_aux_output_end(struct perf_output_handle *handle, unsigned long size,
967 bool truncated) { }
968static inline int
969perf_aux_output_skip(struct perf_output_handle *handle,
970 unsigned long size) { return -EINVAL; }
971static inline void *
972perf_get_aux(struct perf_output_handle *handle) { return NULL; }
0793a61d 973static inline void
ff303e66
PZ
974perf_event_task_migrate(struct task_struct *task) { }
975static inline void
ab0cce56
JO
976perf_event_task_sched_in(struct task_struct *prev,
977 struct task_struct *task) { }
978static inline void
979perf_event_task_sched_out(struct task_struct *prev,
980 struct task_struct *next) { }
cdd6c482
IM
981static inline int perf_event_init_task(struct task_struct *child) { return 0; }
982static inline void perf_event_exit_task(struct task_struct *child) { }
983static inline void perf_event_free_task(struct task_struct *task) { }
4e231c79 984static inline void perf_event_delayed_put(struct task_struct *task) { }
ffe8690c
KX
985static inline struct perf_event *perf_event_get(unsigned int fd) { return ERR_PTR(-EINVAL); }
986static inline const struct perf_event_attr *perf_event_attrs(struct perf_event *event)
987{
988 return ERR_PTR(-EINVAL);
989}
990static inline u64 perf_event_read_local(struct perf_event *event) { return -EINVAL; }
57c0c15b 991static inline void perf_event_print_debug(void) { }
57c0c15b
IM
992static inline int perf_event_task_disable(void) { return -EINVAL; }
993static inline int perf_event_task_enable(void) { return -EINVAL; }
26ca5c11
AK
994static inline int perf_event_refresh(struct perf_event *event, int refresh)
995{
996 return -EINVAL;
997}
15dbf27c 998
925d519a 999static inline void
a8b0ca17 1000perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr) { }
24f1e32c 1001static inline void
86038c5e
PZI
1002perf_sw_event_sched(u32 event_id, u64 nr, u64 addr) { }
1003static inline void
184f412c 1004perf_bp_event(struct perf_event *event, void *data) { }
0a4a9391 1005
39447b38 1006static inline int perf_register_guest_info_callbacks
e7e7ee2e 1007(struct perf_guest_info_callbacks *callbacks) { return 0; }
39447b38 1008static inline int perf_unregister_guest_info_callbacks
e7e7ee2e 1009(struct perf_guest_info_callbacks *callbacks) { return 0; }
39447b38 1010
57c0c15b 1011static inline void perf_event_mmap(struct vm_area_struct *vma) { }
e041e328 1012static inline void perf_event_exec(void) { }
82b89778 1013static inline void perf_event_comm(struct task_struct *tsk, bool exec) { }
cdd6c482
IM
1014static inline void perf_event_fork(struct task_struct *tsk) { }
1015static inline void perf_event_init(void) { }
184f412c 1016static inline int perf_swevent_get_recursion_context(void) { return -1; }
4ed7c92d 1017static inline void perf_swevent_put_recursion_context(int rctx) { }
ab573844 1018static inline u64 perf_swevent_set_period(struct perf_event *event) { return 0; }
44234adc
FW
1019static inline void perf_event_enable(struct perf_event *event) { }
1020static inline void perf_event_disable(struct perf_event *event) { }
500ad2d8 1021static inline int __perf_event_disable(void *info) { return -1; }
e9d2b064 1022static inline void perf_event_task_tick(void) { }
ffe8690c 1023static inline int perf_event_release_kernel(struct perf_event *event) { return 0; }
0793a61d
TG
1024#endif
1025
026249ef
FW
1026#if defined(CONFIG_PERF_EVENTS) && defined(CONFIG_NO_HZ_FULL)
1027extern bool perf_event_can_stop_tick(void);
1028#else
1029static inline bool perf_event_can_stop_tick(void) { return true; }
1030#endif
1031
6c4d3bc9
DR
1032#if defined(CONFIG_PERF_EVENTS) && defined(CONFIG_CPU_SUP_INTEL)
1033extern void perf_restore_debug_store(void);
1034#else
1d9d8639 1035static inline void perf_restore_debug_store(void) { }
0793a61d
TG
1036#endif
1037
e7e7ee2e 1038#define perf_output_put(handle, x) perf_output_copy((handle), &(x), sizeof(x))
5622f295 1039
3f6da390 1040/*
0a0fca9d 1041 * This has to have a higher priority than migration_notifier in sched/core.c.
3f6da390 1042 */
e7e7ee2e
IM
1043#define perf_cpu_notifier(fn) \
1044do { \
0db0628d 1045 static struct notifier_block fn##_nb = \
e7e7ee2e 1046 { .notifier_call = fn, .priority = CPU_PRI_PERF }; \
c13d38e4 1047 unsigned long cpu = smp_processor_id(); \
6760bca9 1048 unsigned long flags; \
f0bdb5e0
SB
1049 \
1050 cpu_notifier_register_begin(); \
e7e7ee2e 1051 fn(&fn##_nb, (unsigned long)CPU_UP_PREPARE, \
c13d38e4 1052 (void *)(unsigned long)cpu); \
6760bca9 1053 local_irq_save(flags); \
e7e7ee2e 1054 fn(&fn##_nb, (unsigned long)CPU_STARTING, \
c13d38e4 1055 (void *)(unsigned long)cpu); \
6760bca9 1056 local_irq_restore(flags); \
e7e7ee2e 1057 fn(&fn##_nb, (unsigned long)CPU_ONLINE, \
c13d38e4 1058 (void *)(unsigned long)cpu); \
f0bdb5e0
SB
1059 __register_cpu_notifier(&fn##_nb); \
1060 cpu_notifier_register_done(); \
3f6da390
PZ
1061} while (0)
1062
f0bdb5e0
SB
1063/*
1064 * Bare-bones version of perf_cpu_notifier(), which doesn't invoke the
1065 * callback for already online CPUs.
1066 */
1067#define __perf_cpu_notifier(fn) \
1068do { \
1069 static struct notifier_block fn##_nb = \
1070 { .notifier_call = fn, .priority = CPU_PRI_PERF }; \
1071 \
1072 __register_cpu_notifier(&fn##_nb); \
1073} while (0)
641cc938 1074
2663960c
SB
1075struct perf_pmu_events_attr {
1076 struct device_attribute attr;
1077 u64 id;
3a54aaa0 1078 const char *event_str;
2663960c
SB
1079};
1080
fd979c01
CS
1081ssize_t perf_event_sysfs_show(struct device *dev, struct device_attribute *attr,
1082 char *page);
1083
2663960c
SB
1084#define PMU_EVENT_ATTR(_name, _var, _id, _show) \
1085static struct perf_pmu_events_attr _var = { \
1086 .attr = __ATTR(_name, 0444, _show, NULL), \
1087 .id = _id, \
1088};
1089
f0405b81
CS
1090#define PMU_EVENT_ATTR_STRING(_name, _var, _str) \
1091static struct perf_pmu_events_attr _var = { \
1092 .attr = __ATTR(_name, 0444, perf_event_sysfs_show, NULL), \
1093 .id = 0, \
1094 .event_str = _str, \
1095};
1096
641cc938
JO
1097#define PMU_FORMAT_ATTR(_name, _format) \
1098static ssize_t \
1099_name##_show(struct device *dev, \
1100 struct device_attribute *attr, \
1101 char *page) \
1102{ \
1103 BUILD_BUG_ON(sizeof(_format) >= PAGE_SIZE); \
1104 return sprintf(page, _format "\n"); \
1105} \
1106 \
1107static struct device_attribute format_attr_##_name = __ATTR_RO(_name)
1108
cdd6c482 1109#endif /* _LINUX_PERF_EVENT_H */