perf_event.h 26 KB

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