perf_event.h 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928
  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 {
  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_EXIT = -3,
  238. PERF_EVENT_STATE_ERROR = -2,
  239. PERF_EVENT_STATE_OFF = -1,
  240. PERF_EVENT_STATE_INACTIVE = 0,
  241. PERF_EVENT_STATE_ACTIVE = 1,
  242. };
  243. struct file;
  244. struct perf_sample_data;
  245. typedef void (*perf_overflow_handler_t)(struct perf_event *,
  246. struct perf_sample_data *,
  247. struct pt_regs *regs);
  248. enum perf_group_flag {
  249. PERF_GROUP_SOFTWARE = 0x1,
  250. };
  251. #define SWEVENT_HLIST_BITS 8
  252. #define SWEVENT_HLIST_SIZE (1 << SWEVENT_HLIST_BITS)
  253. struct swevent_hlist {
  254. struct hlist_head heads[SWEVENT_HLIST_SIZE];
  255. struct rcu_head rcu_head;
  256. };
  257. #define PERF_ATTACH_CONTEXT 0x01
  258. #define PERF_ATTACH_GROUP 0x02
  259. #define PERF_ATTACH_TASK 0x04
  260. struct perf_cgroup;
  261. struct ring_buffer;
  262. /**
  263. * struct perf_event - performance event kernel representation:
  264. */
  265. struct perf_event {
  266. #ifdef CONFIG_PERF_EVENTS
  267. /*
  268. * entry onto perf_event_context::event_list;
  269. * modifications require ctx->lock
  270. * RCU safe iterations.
  271. */
  272. struct list_head event_entry;
  273. /*
  274. * XXX: group_entry and sibling_list should be mutually exclusive;
  275. * either you're a sibling on a group, or you're the group leader.
  276. * Rework the code to always use the same list element.
  277. *
  278. * Locked for modification by both ctx->mutex and ctx->lock; holding
  279. * either sufficies for read.
  280. */
  281. struct list_head group_entry;
  282. struct list_head sibling_list;
  283. /*
  284. * We need storage to track the entries in perf_pmu_migrate_context; we
  285. * cannot use the event_entry because of RCU and we want to keep the
  286. * group in tact which avoids us using the other two entries.
  287. */
  288. struct list_head migrate_entry;
  289. struct hlist_node hlist_entry;
  290. struct list_head active_entry;
  291. int nr_siblings;
  292. int group_flags;
  293. struct perf_event *group_leader;
  294. struct pmu *pmu;
  295. enum perf_event_active_state state;
  296. unsigned int attach_state;
  297. local64_t count;
  298. atomic64_t child_count;
  299. /*
  300. * These are the total time in nanoseconds that the event
  301. * has been enabled (i.e. eligible to run, and the task has
  302. * been scheduled in, if this is a per-task event)
  303. * and running (scheduled onto the CPU), respectively.
  304. *
  305. * They are computed from tstamp_enabled, tstamp_running and
  306. * tstamp_stopped when the event is in INACTIVE or ACTIVE state.
  307. */
  308. u64 total_time_enabled;
  309. u64 total_time_running;
  310. /*
  311. * These are timestamps used for computing total_time_enabled
  312. * and total_time_running when the event is in INACTIVE or
  313. * ACTIVE state, measured in nanoseconds from an arbitrary point
  314. * in time.
  315. * tstamp_enabled: the notional time when the event was enabled
  316. * tstamp_running: the notional time when the event was scheduled on
  317. * tstamp_stopped: in INACTIVE state, the notional time when the
  318. * event was scheduled off.
  319. */
  320. u64 tstamp_enabled;
  321. u64 tstamp_running;
  322. u64 tstamp_stopped;
  323. /*
  324. * timestamp shadows the actual context timing but it can
  325. * be safely used in NMI interrupt context. It reflects the
  326. * context time as it was when the event was last scheduled in.
  327. *
  328. * ctx_time already accounts for ctx->timestamp. Therefore to
  329. * compute ctx_time for a sample, simply add perf_clock().
  330. */
  331. u64 shadow_ctx_time;
  332. struct perf_event_attr attr;
  333. u16 header_size;
  334. u16 id_header_size;
  335. u16 read_size;
  336. struct hw_perf_event hw;
  337. struct perf_event_context *ctx;
  338. atomic_long_t refcount;
  339. /*
  340. * These accumulate total time (in nanoseconds) that children
  341. * events have been enabled and running, respectively.
  342. */
  343. atomic64_t child_total_time_enabled;
  344. atomic64_t child_total_time_running;
  345. /*
  346. * Protect attach/detach and child_list:
  347. */
  348. struct mutex child_mutex;
  349. struct list_head child_list;
  350. struct perf_event *parent;
  351. int oncpu;
  352. int cpu;
  353. struct list_head owner_entry;
  354. struct task_struct *owner;
  355. /* mmap bits */
  356. struct mutex mmap_mutex;
  357. atomic_t mmap_count;
  358. struct ring_buffer *rb;
  359. struct list_head rb_entry;
  360. unsigned long rcu_batches;
  361. int rcu_pending;
  362. /* poll related */
  363. wait_queue_head_t waitq;
  364. struct fasync_struct *fasync;
  365. /* delayed work for NMIs and such */
  366. int pending_wakeup;
  367. int pending_kill;
  368. int pending_disable;
  369. struct irq_work pending;
  370. atomic_t event_limit;
  371. void (*destroy)(struct perf_event *);
  372. struct rcu_head rcu_head;
  373. struct pid_namespace *ns;
  374. u64 id;
  375. perf_overflow_handler_t overflow_handler;
  376. void *overflow_handler_context;
  377. #ifdef CONFIG_EVENT_TRACING
  378. struct ftrace_event_call *tp_event;
  379. struct event_filter *filter;
  380. #ifdef CONFIG_FUNCTION_TRACER
  381. struct ftrace_ops ftrace_ops;
  382. #endif
  383. #endif
  384. #ifdef CONFIG_CGROUP_PERF
  385. struct perf_cgroup *cgrp; /* cgroup event is attach to */
  386. int cgrp_defer_enabled;
  387. #endif
  388. #endif /* CONFIG_PERF_EVENTS */
  389. };
  390. enum perf_event_context_type {
  391. task_context,
  392. cpu_context,
  393. };
  394. /**
  395. * struct perf_event_context - event context structure
  396. *
  397. * Used as a container for task events and CPU events as well:
  398. */
  399. struct perf_event_context {
  400. struct pmu *pmu;
  401. enum perf_event_context_type type;
  402. /*
  403. * Protect the states of the events in the list,
  404. * nr_active, and the list:
  405. */
  406. raw_spinlock_t lock;
  407. /*
  408. * Protect the list of events. Locking either mutex or lock
  409. * is sufficient to ensure the list doesn't change; to change
  410. * the list you need to lock both the mutex and the spinlock.
  411. */
  412. struct mutex mutex;
  413. struct list_head pinned_groups;
  414. struct list_head flexible_groups;
  415. struct list_head event_list;
  416. int nr_events;
  417. int nr_active;
  418. int is_active;
  419. int nr_stat;
  420. int nr_freq;
  421. int rotate_disable;
  422. atomic_t refcount;
  423. struct task_struct *task;
  424. /*
  425. * Context clock, runs when context enabled.
  426. */
  427. u64 time;
  428. u64 timestamp;
  429. /*
  430. * These fields let us detect when two contexts have both
  431. * been cloned (inherited) from a common ancestor.
  432. */
  433. struct perf_event_context *parent_ctx;
  434. u64 parent_gen;
  435. u64 generation;
  436. int pin_count;
  437. int nr_cgroups; /* cgroup evts */
  438. int nr_branch_stack; /* branch_stack evt */
  439. struct rcu_head rcu_head;
  440. struct delayed_work orphans_remove;
  441. bool orphans_remove_sched;
  442. };
  443. /*
  444. * Number of contexts where an event can trigger:
  445. * task, softirq, hardirq, nmi.
  446. */
  447. #define PERF_NR_CONTEXTS 4
  448. /**
  449. * struct perf_event_cpu_context - per cpu event context structure
  450. */
  451. struct perf_cpu_context {
  452. struct perf_event_context ctx;
  453. struct perf_event_context *task_ctx;
  454. int active_oncpu;
  455. int exclusive;
  456. struct hrtimer hrtimer;
  457. ktime_t hrtimer_interval;
  458. struct list_head rotation_list;
  459. struct pmu *unique_pmu;
  460. struct perf_cgroup *cgrp;
  461. };
  462. struct perf_output_handle {
  463. struct perf_event *event;
  464. struct ring_buffer *rb;
  465. unsigned long wakeup;
  466. unsigned long size;
  467. void *addr;
  468. int page;
  469. };
  470. #ifdef CONFIG_PERF_EVENTS
  471. extern int perf_pmu_register(struct pmu *pmu, const char *name, int type);
  472. extern void perf_pmu_unregister(struct pmu *pmu);
  473. extern int perf_num_counters(void);
  474. extern const char *perf_pmu_name(void);
  475. extern void __perf_event_task_sched_in(struct task_struct *prev,
  476. struct task_struct *task);
  477. extern void __perf_event_task_sched_out(struct task_struct *prev,
  478. struct task_struct *next);
  479. extern int perf_event_init_task(struct task_struct *child);
  480. extern void perf_event_exit_task(struct task_struct *child);
  481. extern void perf_event_free_task(struct task_struct *task);
  482. extern void perf_event_delayed_put(struct task_struct *task);
  483. extern void perf_event_print_debug(void);
  484. extern void perf_pmu_disable(struct pmu *pmu);
  485. extern void perf_pmu_enable(struct pmu *pmu);
  486. extern int perf_event_task_disable(void);
  487. extern int perf_event_task_enable(void);
  488. extern int perf_event_refresh(struct perf_event *event, int refresh);
  489. extern void perf_event_update_userpage(struct perf_event *event);
  490. extern int perf_event_release_kernel(struct perf_event *event);
  491. extern struct perf_event *
  492. perf_event_create_kernel_counter(struct perf_event_attr *attr,
  493. int cpu,
  494. struct task_struct *task,
  495. perf_overflow_handler_t callback,
  496. void *context);
  497. extern void perf_pmu_migrate_context(struct pmu *pmu,
  498. int src_cpu, int dst_cpu);
  499. extern u64 perf_event_read_value(struct perf_event *event,
  500. u64 *enabled, u64 *running);
  501. struct perf_sample_data {
  502. /*
  503. * Fields set by perf_sample_data_init(), group so as to
  504. * minimize the cachelines touched.
  505. */
  506. u64 addr;
  507. struct perf_raw_record *raw;
  508. struct perf_branch_stack *br_stack;
  509. u64 period;
  510. u64 weight;
  511. u64 txn;
  512. union perf_mem_data_src data_src;
  513. /*
  514. * The other fields, optionally {set,used} by
  515. * perf_{prepare,output}_sample().
  516. */
  517. u64 type;
  518. u64 ip;
  519. struct {
  520. u32 pid;
  521. u32 tid;
  522. } tid_entry;
  523. u64 time;
  524. u64 id;
  525. u64 stream_id;
  526. struct {
  527. u32 cpu;
  528. u32 reserved;
  529. } cpu_entry;
  530. struct perf_callchain_entry *callchain;
  531. struct perf_regs regs_user;
  532. struct perf_regs regs_intr;
  533. u64 stack_user_size;
  534. } ____cacheline_aligned;
  535. /* default value for data source */
  536. #define PERF_MEM_NA (PERF_MEM_S(OP, NA) |\
  537. PERF_MEM_S(LVL, NA) |\
  538. PERF_MEM_S(SNOOP, NA) |\
  539. PERF_MEM_S(LOCK, NA) |\
  540. PERF_MEM_S(TLB, NA))
  541. static inline void perf_sample_data_init(struct perf_sample_data *data,
  542. u64 addr, u64 period)
  543. {
  544. /* remaining struct members initialized in perf_prepare_sample() */
  545. data->addr = addr;
  546. data->raw = NULL;
  547. data->br_stack = NULL;
  548. data->period = period;
  549. data->weight = 0;
  550. data->data_src.val = PERF_MEM_NA;
  551. data->txn = 0;
  552. }
  553. extern void perf_output_sample(struct perf_output_handle *handle,
  554. struct perf_event_header *header,
  555. struct perf_sample_data *data,
  556. struct perf_event *event);
  557. extern void perf_prepare_sample(struct perf_event_header *header,
  558. struct perf_sample_data *data,
  559. struct perf_event *event,
  560. struct pt_regs *regs);
  561. extern int perf_event_overflow(struct perf_event *event,
  562. struct perf_sample_data *data,
  563. struct pt_regs *regs);
  564. static inline bool is_sampling_event(struct perf_event *event)
  565. {
  566. return event->attr.sample_period != 0;
  567. }
  568. /*
  569. * Return 1 for a software event, 0 for a hardware event
  570. */
  571. static inline int is_software_event(struct perf_event *event)
  572. {
  573. return event->pmu->task_ctx_nr == perf_sw_context;
  574. }
  575. extern struct static_key perf_swevent_enabled[PERF_COUNT_SW_MAX];
  576. extern void __perf_sw_event(u32, u64, struct pt_regs *, u64);
  577. #ifndef perf_arch_fetch_caller_regs
  578. static inline void perf_arch_fetch_caller_regs(struct pt_regs *regs, unsigned long ip) { }
  579. #endif
  580. /*
  581. * Take a snapshot of the regs. Skip ip and frame pointer to
  582. * the nth caller. We only need a few of the regs:
  583. * - ip for PERF_SAMPLE_IP
  584. * - cs for user_mode() tests
  585. * - bp for callchains
  586. * - eflags, for future purposes, just in case
  587. */
  588. static inline void perf_fetch_caller_regs(struct pt_regs *regs)
  589. {
  590. memset(regs, 0, sizeof(*regs));
  591. perf_arch_fetch_caller_regs(regs, CALLER_ADDR0);
  592. }
  593. static __always_inline void
  594. perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr)
  595. {
  596. struct pt_regs hot_regs;
  597. if (static_key_false(&perf_swevent_enabled[event_id])) {
  598. if (!regs) {
  599. perf_fetch_caller_regs(&hot_regs);
  600. regs = &hot_regs;
  601. }
  602. __perf_sw_event(event_id, nr, regs, addr);
  603. }
  604. }
  605. extern struct static_key_deferred perf_sched_events;
  606. static inline void perf_event_task_sched_in(struct task_struct *prev,
  607. struct task_struct *task)
  608. {
  609. if (static_key_false(&perf_sched_events.key))
  610. __perf_event_task_sched_in(prev, task);
  611. }
  612. static inline void perf_event_task_sched_out(struct task_struct *prev,
  613. struct task_struct *next)
  614. {
  615. perf_sw_event(PERF_COUNT_SW_CONTEXT_SWITCHES, 1, NULL, 0);
  616. if (static_key_false(&perf_sched_events.key))
  617. __perf_event_task_sched_out(prev, next);
  618. }
  619. extern void perf_event_mmap(struct vm_area_struct *vma);
  620. extern struct perf_guest_info_callbacks *perf_guest_cbs;
  621. extern int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *callbacks);
  622. extern int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *callbacks);
  623. extern void perf_event_exec(void);
  624. extern void perf_event_comm(struct task_struct *tsk, bool exec);
  625. extern void perf_event_fork(struct task_struct *tsk);
  626. /* Callchains */
  627. DECLARE_PER_CPU(struct perf_callchain_entry, perf_callchain_entry);
  628. extern void perf_callchain_user(struct perf_callchain_entry *entry, struct pt_regs *regs);
  629. extern void perf_callchain_kernel(struct perf_callchain_entry *entry, struct pt_regs *regs);
  630. static inline void perf_callchain_store(struct perf_callchain_entry *entry, u64 ip)
  631. {
  632. if (entry->nr < PERF_MAX_STACK_DEPTH)
  633. entry->ip[entry->nr++] = ip;
  634. }
  635. extern int sysctl_perf_event_paranoid;
  636. extern int sysctl_perf_event_mlock;
  637. extern int sysctl_perf_event_sample_rate;
  638. extern int sysctl_perf_cpu_time_max_percent;
  639. extern void perf_sample_event_took(u64 sample_len_ns);
  640. extern int perf_proc_update_handler(struct ctl_table *table, int write,
  641. void __user *buffer, size_t *lenp,
  642. loff_t *ppos);
  643. extern int perf_cpu_time_max_percent_handler(struct ctl_table *table, int write,
  644. void __user *buffer, size_t *lenp,
  645. loff_t *ppos);
  646. static inline bool perf_paranoid_tracepoint_raw(void)
  647. {
  648. return sysctl_perf_event_paranoid > -1;
  649. }
  650. static inline bool perf_paranoid_cpu(void)
  651. {
  652. return sysctl_perf_event_paranoid > 0;
  653. }
  654. static inline bool perf_paranoid_kernel(void)
  655. {
  656. return sysctl_perf_event_paranoid > 1;
  657. }
  658. extern void perf_event_init(void);
  659. extern void perf_tp_event(u64 addr, u64 count, void *record,
  660. int entry_size, struct pt_regs *regs,
  661. struct hlist_head *head, int rctx,
  662. struct task_struct *task);
  663. extern void perf_bp_event(struct perf_event *event, void *data);
  664. #ifndef perf_misc_flags
  665. # define perf_misc_flags(regs) \
  666. (user_mode(regs) ? PERF_RECORD_MISC_USER : PERF_RECORD_MISC_KERNEL)
  667. # define perf_instruction_pointer(regs) instruction_pointer(regs)
  668. #endif
  669. static inline bool has_branch_stack(struct perf_event *event)
  670. {
  671. return event->attr.sample_type & PERF_SAMPLE_BRANCH_STACK;
  672. }
  673. extern int perf_output_begin(struct perf_output_handle *handle,
  674. struct perf_event *event, unsigned int size);
  675. extern void perf_output_end(struct perf_output_handle *handle);
  676. extern unsigned int perf_output_copy(struct perf_output_handle *handle,
  677. const void *buf, unsigned int len);
  678. extern unsigned int perf_output_skip(struct perf_output_handle *handle,
  679. unsigned int len);
  680. extern int perf_swevent_get_recursion_context(void);
  681. extern void perf_swevent_put_recursion_context(int rctx);
  682. extern u64 perf_swevent_set_period(struct perf_event *event);
  683. extern void perf_event_enable(struct perf_event *event);
  684. extern void perf_event_disable(struct perf_event *event);
  685. extern int __perf_event_disable(void *info);
  686. extern void perf_event_task_tick(void);
  687. #else /* !CONFIG_PERF_EVENTS: */
  688. static inline void
  689. perf_event_task_sched_in(struct task_struct *prev,
  690. struct task_struct *task) { }
  691. static inline void
  692. perf_event_task_sched_out(struct task_struct *prev,
  693. struct task_struct *next) { }
  694. static inline int perf_event_init_task(struct task_struct *child) { return 0; }
  695. static inline void perf_event_exit_task(struct task_struct *child) { }
  696. static inline void perf_event_free_task(struct task_struct *task) { }
  697. static inline void perf_event_delayed_put(struct task_struct *task) { }
  698. static inline void perf_event_print_debug(void) { }
  699. static inline int perf_event_task_disable(void) { return -EINVAL; }
  700. static inline int perf_event_task_enable(void) { return -EINVAL; }
  701. static inline int perf_event_refresh(struct perf_event *event, int refresh)
  702. {
  703. return -EINVAL;
  704. }
  705. static inline void
  706. perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr) { }
  707. static inline void
  708. perf_bp_event(struct perf_event *event, void *data) { }
  709. static inline int perf_register_guest_info_callbacks
  710. (struct perf_guest_info_callbacks *callbacks) { return 0; }
  711. static inline int perf_unregister_guest_info_callbacks
  712. (struct perf_guest_info_callbacks *callbacks) { return 0; }
  713. static inline void perf_event_mmap(struct vm_area_struct *vma) { }
  714. static inline void perf_event_exec(void) { }
  715. static inline void perf_event_comm(struct task_struct *tsk, bool exec) { }
  716. static inline void perf_event_fork(struct task_struct *tsk) { }
  717. static inline void perf_event_init(void) { }
  718. static inline int perf_swevent_get_recursion_context(void) { return -1; }
  719. static inline void perf_swevent_put_recursion_context(int rctx) { }
  720. static inline u64 perf_swevent_set_period(struct perf_event *event) { return 0; }
  721. static inline void perf_event_enable(struct perf_event *event) { }
  722. static inline void perf_event_disable(struct perf_event *event) { }
  723. static inline int __perf_event_disable(void *info) { return -1; }
  724. static inline void perf_event_task_tick(void) { }
  725. #endif
  726. #if defined(CONFIG_PERF_EVENTS) && defined(CONFIG_NO_HZ_FULL)
  727. extern bool perf_event_can_stop_tick(void);
  728. #else
  729. static inline bool perf_event_can_stop_tick(void) { return true; }
  730. #endif
  731. #if defined(CONFIG_PERF_EVENTS) && defined(CONFIG_CPU_SUP_INTEL)
  732. extern void perf_restore_debug_store(void);
  733. #else
  734. static inline void perf_restore_debug_store(void) { }
  735. #endif
  736. #define perf_output_put(handle, x) perf_output_copy((handle), &(x), sizeof(x))
  737. /*
  738. * This has to have a higher priority than migration_notifier in sched/core.c.
  739. */
  740. #define perf_cpu_notifier(fn) \
  741. do { \
  742. static struct notifier_block fn##_nb = \
  743. { .notifier_call = fn, .priority = CPU_PRI_PERF }; \
  744. unsigned long cpu = smp_processor_id(); \
  745. unsigned long flags; \
  746. \
  747. cpu_notifier_register_begin(); \
  748. fn(&fn##_nb, (unsigned long)CPU_UP_PREPARE, \
  749. (void *)(unsigned long)cpu); \
  750. local_irq_save(flags); \
  751. fn(&fn##_nb, (unsigned long)CPU_STARTING, \
  752. (void *)(unsigned long)cpu); \
  753. local_irq_restore(flags); \
  754. fn(&fn##_nb, (unsigned long)CPU_ONLINE, \
  755. (void *)(unsigned long)cpu); \
  756. __register_cpu_notifier(&fn##_nb); \
  757. cpu_notifier_register_done(); \
  758. } while (0)
  759. /*
  760. * Bare-bones version of perf_cpu_notifier(), which doesn't invoke the
  761. * callback for already online CPUs.
  762. */
  763. #define __perf_cpu_notifier(fn) \
  764. do { \
  765. static struct notifier_block fn##_nb = \
  766. { .notifier_call = fn, .priority = CPU_PRI_PERF }; \
  767. \
  768. __register_cpu_notifier(&fn##_nb); \
  769. } while (0)
  770. struct perf_pmu_events_attr {
  771. struct device_attribute attr;
  772. u64 id;
  773. const char *event_str;
  774. };
  775. ssize_t perf_event_sysfs_show(struct device *dev, struct device_attribute *attr,
  776. char *page);
  777. #define PMU_EVENT_ATTR(_name, _var, _id, _show) \
  778. static struct perf_pmu_events_attr _var = { \
  779. .attr = __ATTR(_name, 0444, _show, NULL), \
  780. .id = _id, \
  781. };
  782. #define PMU_EVENT_ATTR_STRING(_name, _var, _str) \
  783. static struct perf_pmu_events_attr _var = { \
  784. .attr = __ATTR(_name, 0444, perf_event_sysfs_show, NULL), \
  785. .id = 0, \
  786. .event_str = _str, \
  787. };
  788. #define PMU_FORMAT_ATTR(_name, _format) \
  789. static ssize_t \
  790. _name##_show(struct device *dev, \
  791. struct device_attribute *attr, \
  792. char *page) \
  793. { \
  794. BUILD_BUG_ON(sizeof(_format) >= PAGE_SIZE); \
  795. return sprintf(page, _format "\n"); \
  796. } \
  797. \
  798. static struct device_attribute format_attr_##_name = __ATTR_RO(_name)
  799. #endif /* _LINUX_PERF_EVENT_H */