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