perf_event.h 25 KB

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